简体   繁体   中英

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.

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

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.

You can simply extract the object from the array and modify it in the first loop.

 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))
    });
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM