as you can see if you check the data source it has a key isEdit and it has a boolean value. We compare dataSouce value to tempValues as you can see the column value from data source matches the keys on the tempValues. After we compare we check if the value from the datasource which is isEdit is true or fals
if the value of isEdit is true get value from tempValues.dealIdleDetailsForFinanceDto so for example NVB isEdit value is true so the nvb value from tempValues.dealIdleDetailsForFinanceDto is 99 else it is 20
Check value of final output.
Please see final output for comparison. Thanks.
#data 1
dataSouce = [
{
"name": "NVB",
"value": 0,
"financeValue": 0,
"column": "nvb",
"isEdit": true
},
{
"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": 88,
}
}
#Sample Final Output
[
{
"name": "NVB",
"value": 99,
"financeValue": 20,
"column": "nvb",
"isEdit": true
},
{
"name": "ROU",
"value": 100,
"financeValue": 100,
"column": "rou",
"isEdit": false
},
{
"name": "Net Present Value",
"value": 88,
"financeValue": 50,
"column": "netPresentValue",
"isEdit": true
}
]
#sample code
const finalOutput = dataSource.map(item => {
// Look up the value in tempValues using the column property of the object
if(item.isEdit) {
}
const financeValue = tempValues[item.column];
// Return a new object with the updated value property
return {
...item,
financeValue,
};
});
CodePudding user response:
Assuming dataSouce[2].isEdit = true
, and that this is a typo in the question, then the following code snippet should help.
dataSource = [
{
"name": "NVB",
"value": 0,
"financeValue": 0,
"column": "nvb",
"isEdit": true
},
{
"name": "ROU",
"value": 0,
"financeValue": 0,
"column": "rou",
"isEdit": false
},
{
"name": "Net Present Value",
"value": 0,
"financeValue": 0,
"column": "netPresentValue",
"isEdit": true
},
];
tempValues = {
"transactionId": 20,
"nvb": 20,
"rou": 100,
"netPresentValue": 50,
"dealIdleDetailsForFinanceDto": {
"nvb": 99,
"rou": 4,
"netPresentValue": 88,
}
};
const finalOutput = dataSource.map(item => {
const tempValue = tempValues[item.column];
const dealIdleDetailsValue = tempValues.dealIdleDetailsForFinanceDto[item.column];
return {
...item,
financeValue: tempValue,
value: item.isEdit ? dealIdleDetailsValue : tempValue
}
});
console.log(finalOutput);