[英]How to change the value of property of object which is inside the array
I am trying to change the value of property of object which is inside the array and that array also inside one object.我正在尝试更改 object 的属性值,该属性位于数组内部,并且该数组也在一个 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这给了我 output 作为
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提前致谢
You're running the second loop after the first loop is done, so the variables QUALITYNAME
and rates_a1
have the values from the last iteration.您在第一个循环完成后运行第二个循环,因此变量
QUALITYNAME
和rates_a1
具有上次迭代的值。
You can simply extract the object from the array and modify it in the first loop.您可以简单地从数组中提取 object 并在第一个循环中对其进行修改。
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[0]; if (result) { result.VALUE = parseFloat(result.VALUE.toFixed(2)); } arr[i].RATES = result; } console.log(arr);
If I understand it correctly you're just trying to round all rates to two decimals, you can achieve that with a nested loop like:如果我理解正确,您只是想将所有费率四舍五入到小数点后两位,您可以使用如下嵌套循环来实现:
var arr = [{
QUALITYNAME: "Berry Fancy",
RATES: [{
"UNIT": "LB",
"CURRENCY": "USD",
"VALUE": 6.205240232694746
}]
},
{
QUALITYNAME: "Berry USDA",
RATES: [{
"UNIT": "LB",
"CURRENCY": "USD",
"VALUE": 5.622770183585882
}]
}
]
// Loop through each array item
arr.forEach((item) => {
// Loop through each rate inside the item
item.RATES.forEach((rate) => {
// Round to two decimals
rate.VALUE = parseFloat(rate.VALUE.toFixed(2))
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.