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
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, "dealIdleDetailsForFinanceDto": { "nvb": 99, "rou": 4, "netPresentValue": 0, } }
#FinalOutput
= [
{
"name": "NVB",
"value": 20,
"financeValue": 99,
"column": "nvb",
"isEdit": false
},
{
"name": "ROU",
"value": 100,
"financeValue": 4,
"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?
CodePudding user response:
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.