简体   繁体   English

如何更改数组内 object 的属性值

[英]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.您在第一个循环完成后运行第二个循环,因此变量QUALITYNAMErates_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.

相关问题 尝试更改对象属性内的数组值 - Trying to change array value inside object property 如何使用 JavaScript 或 jQuery 更改数组内的对象的值? - How to change value of object which is inside an array using JavaScript or jQuery? 更改数组内部特定对象的属性的状态值 - Change the state value of the property of a particular object that is inside an array 如果属性与比较值不匹配,如何更改数组中 object 属性的值? - How to change value for object property in array if property not match with compared value? 如何更改数组内对象的值? - How to change value of Object inside an Array? 如何在响应中更改数组内对象的属性 - How to change the property of an object inside an array propertly in react 删除数组内的对象属性,数组内的对象属性,不在数组上循环 - Remove object property which inside an array which inside an object which inside an array without looping on array 无法插入对象属性内的数组 - Unable to interpolate an array which is inside of an object property 如何使用 javascript 中的数组值更改数组 object 中的属性值 - How to change property value in array object with array values in javascript 如何为Javascript对象中的属性设置值,该属性由键数组标识 - How to set value to a property in a Javascript object, which is identified by an array of keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM