Transformed an array of objects by extracting values from a specified object using TS/JS

Hey folks, I'm wondering what's the most effective way to update an array of objects with values from another object. For example, imagine we have an array and an object structured like this:

let arr = [
{
        parameter: 'aaa', 
        isSomething: false
},
{
        parameter: 'bbb', 
        isSomething: false
}, 
{
        parameter: 'ccc', 
        isSomething: false
}
];

let obj = {aaa: false, bbb: true, ccc: true}

The desired output would be:

let arr = [
{
        parameter: 'aaa', 
        isSomething: false
},
{
        parameter: 'bbb', 
        isSomething: true
}, 
{
        parameter: 'ccc', 
        isSomething: true
}
];

I could use some assistance with this task. Any help would be greatly appreciated. Thank you!

Answer №1

If you want to transform an array using the Arra#map() method and assign values using Destructuring assignment

Here is an example of how it can be done:

const arr = [{parameter: 'aaa',isSomething: false},{parameter: 'bbb',isSomething: false},{parameter: 'ccc',isSomething: false}

const obj = {
  aaa: false,
  bbb: true,
  ccc: true
}

const result = arr.map(({ parameter, isSomething }) => ({ 
  parameter, 
  isSomething: obj[parameter]
}))

console.log(result)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Hide an Angular button when a page is loaded, depending on the information found in a table

How can I hide the Submit button in a table row if a certain condition is met based on information from the database? I attempted to create a function that returns true or false, but it ends up crashing my program because it runs continuously. I also trie ...

Combine two items, with one being contained within a list

Is there a simple way to merge 2 objects in PHP (Laravel), where one is in an array and the other is a plain object with a nested object? Edit: The desired result should be a single object [ { "id": 1, "release_date": "1998", "license_plat ...

Determining the appropriate generic type in Typescript

In my code, there is a method designed to extend an existing key-value map with objects of the same type. This can be useful when working with database query results. export function extendWith< T extends { id: string | number }, O = | (T[" ...

How do you extract a JSON response from a variable?

When I use console.log(data) from within a function that receives the result of an AJAX request in Google Chrome developer tools, I am puzzled about how to access the value of 'result'. It seems like the entire response is being treated as a stri ...

The Node.js Express Server runs perfectly on my own machine, but encounters issues when deployed to another server

I've encountered a strange issue. My node.js server runs perfectly fine on my local machine, but when I SSH into a digital ocean server and try to run it there, I get this error. I used flightplan to transfer the files to the new machine. deploy@myse ...

Steps for leveraging pdfMake with live data

Recently delving into Angular, I've been exploring the capabilities of pdfMake. While I successfully incorporated static data, I'm facing challenges when attempting to utilize dynamic data. Any guidance on how to achieve this would be greatly app ...

How can a mock document be utilized in a unit test for an imported TypeScript dependency?

To effectively unit-test a legacy TypeScript class, I am seeking ways to mock the document object. The class has dependencies on another class (View.ts), which in turn relies on a 3rd party module that further depends on the existence of the document. The ...

using jQuery, add children to the elements only if there is a match

My goal is to merge children from matching li elements using jQuery. Here's a scenario: First List <ul class="menu1"> <li>Red</li> <ul> <li>apple</li> <li>rose</li> </ul> ...

Leveraging ng-options with various objects in AngularJS

I have an array called 'things' that contains different objects. For example, two different objects are as follows: {name: 'book', value: '5', color: 'blue'} and {name:'pen', length: '10'} ...

Creating multiple countdowns in AngularJS using ng-repeat is a handy feature to have

Currently, I have a small AngularJS application that is designed to search for users and display their upcoming meetings. The data is retrieved from the server in JSON format, with time displayed in UTC for easier calculation to local times. While I have s ...

Error encountered while attempting to validate JWT

This question has been asked multiple times, but I'm still struggling to find the root cause of the issue. I have already signed some data with a token, but when I attempt to verify it, I receive an error message saying "jwt malformed". Interestingly, ...

Extract nested values within objects and arrays, and return the complete type of the original object

I have a dataset that resembles the structure of IconItems: { title: "Category title", description: "Example description", lists: [ { id: "popular", title: "Popular", items: [ { ...

The propagation of onClick events in elements that overlap

Having two divs absolutely positioned overlapping, each containing an onClick handler. The issue is that only the top element's onClick handler fires when clicked. React 17 is being used. Here is some sample code: <div style={{ position: "abs ...

Is requestAnimationFrame necessary for rendering in three.js?

I am currently working on the example provided in Chapter 2 of the WebGL Up and Running book. My goal is to display a static texture-mapped cube. The initial code snippet is not functioning as expected: var camera = null, renderer = null, scene = null ...

tips for transferring a javascript function value to a label within a webform

Currently, I am in search of the latitude and longitude coordinates for a specific address input by the user. Upon clicking a button, the script provided below is triggered to display an alert with the latitude and longitude values: <script type="text/ ...

Creating a clone of a JavaScript object based on an already existing JavaScript object

Can someone help me with this coding issue? var originalObject = { method1: function() { // Do something }, method2: ['a','b','c'], method3: 1234 } I am trying to create a new object based on the orig ...

Guidelines for creating animation for a single point in SVG Polygon

Is there a way to animate the movement of a single polygon point within an SVG using velocity.js? Your assistance is greatly appreciated! <p>Changing...</p> <svg height="250" width="500"> <polygon points="0,0 200,0 200,200 00,20 ...

Electric LocalData

I am currently developing an app that includes a To Do List feature. I am having trouble with saving the tasks and would appreciate some help. I want the tasks to be automatically saved every time you click on "Create Task" and should also be displayed eve ...

Handlebars If the length of variable is more than

I'm currently working on creating email templates using Foundation email and Handlebars. My goal is to display certain headings based on the data passed to the component, but I haven't been successful so far. Can you help me identify what I am do ...

What would be an effective method for sending a multitude of parameters to a controller?

I am currently working on an application that utilizes Java with the Spring framework and Javascript with AngularJs framework. The application features a table displaying a list of objects along with two text fields for filtering these objects. The filteri ...