Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

match and assign value from array and objects

I have a datasource data which is an array of object and it has Column. How do we match it to the tempValues object and get the tempValues values that matches the column on dataSource.

The finalOuput is provided below on how the final ouput should look like . Thanks.

#Data Source

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dataSouce = [
    {
        "name": "NVB",
        "value": 0,
        "financeValue": 0,
        "column": "nvb",
        "isEdit": false
    },
    {
        "name": "ROU",
        "value": 0,
        "financeValue": 0,
        "column": "rou",
        "isEdit": false
    },
    {
        "name": "Net Present Value",
        "value": 0,
        "financeValue": 0,
        "column": "netPresentValue",
        "isEdit": false
    },
]

#tempValues

tempValues = {
    "transactionId": 20,
    "nvb": 20,
    "rou": 100,
    "netPresentValue": 50,
}

#FinalOutput

= [
    {
        "name": "NVB",
        "value": 20,
        "financeValue": 0,
        "column": "nvb",
        "isEdit": false
    },
    {
        "name": "ROU",
        "value": 100,
        "financeValue": 0,
        "column": "rou",
        "isEdit": false
    },
    {
        "name": "Net Present Value",
        "value": 50,
        "financeValue": 0,
        "column": "netPresentValue",
        "isEdit": false
    },
]

How can I match on value and then add value to object?

>Solution :

To match the objects in the dataSource array with the values in the tempValues object and add the matching values to the objects, you could iterate over the dataSource array and use the column property of each object to look up the corresponding value in the tempValues object. You can then update the value property of the object with the matching value from tempValues.

Here is an example of how you could do this:

const finalOutput = dataSource.map(item => {
  // Look up the value in tempValues using the column property of the object
  const value = tempValues[item.column];
  
  // Return a new object with the updated value property
  return {
    ...item,
    value
  };
});

This will produce the following finalOutput array:

[
  {
    "name": "NVB",
    "value": 20,
    "financeValue": 0,
    "column": "nvb",
    "isEdit": false
  },
  {
    "name": "ROU",
    "value": 100,
    "financeValue": 0,
    "column": "rou",
    "isEdit": false
  },
  {
    "name": "Net Present Value",
    "value": 50,
    "financeValue": 0,
    "column": "netPresentValue",
    "isEdit": false
  }
]

This approach uses the Array.prototype.map() method to create a new array by iterating over the dataSource array and transforming each object by adding the matching value from tempValues. The spread operator (…) is used to create a shallow copy of the object and update the value property in the copied object.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading