I am trying to change the value of property of object which is inside the array and that array also inside one object.
I write below code to resolve issue however it take only last value.
my code
var arr = [{
QUALITYNAME: "Berry Fancy",
RATES: [{
"UNIT": "LB",
"CURRENCY": "USD",
"VALUE": 6.205240232694746
}]
},
{
QUALITYNAME: "Berry USDA",
RATES: [{
"UNIT": "LB",
"CURRENCY": "USD",
"VALUE": 5.622770183585882
}]
}
];
var value_a1 = null
for (let i = 0; i < arr.length; i++) {
var result = arr[i].RATES;
var QUALITYNAME = arr[i].QUALITYNAME;
console.log("result", result);
result.forEach((element, index) => {
value_a1 = element.VALUE;
value_a1 = parseFloat(value_a1.toFixed(2))
console.log('value_a1', value_a1);
});
}
arr.forEach(function(item, index) {
arr[index] = {
QUALITYNAME: QUALITYNAME,
RATES: {
"UNIT": "LB",
"CURRENCY": "USD",
"VALUE": value_a1
}
};
});
console.log(arr);
which give me output as
result arr = [ { QUALITYNAME: 'Berry USDA',
RATES: { UNIT: 'LB', CURRENCY: 'USD', VALUE: 5.62 } },
{ QUALITYNAME: 'Berry USDA',
RATES: { UNIT: 'LB', CURRENCY: 'USD', VALUE: 5.62 } } ]
however I need it first value also to be there in result as below
result arr = [ { QUALITYNAME: 'Berry Fancy',
RATES: { UNIT: 'LB', CURRENCY: 'USD', VALUE: 6.21 } },
{ QUALITYNAME: 'Berry USDA',
RATES: { UNIT: 'LB', CURRENCY: 'USD', VALUE: 5.62 } } ]
Please help me to correct this or give me some hint how to solve it.
Thanks in advance
RATESarray with the last element of the array. Why do you need two loops for that?