0

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

2
  • You've taken the array from RATES, so what happens if you get more than one RATE in that list? Commented Jun 9, 2020 at 16:49
  • What are you trying to do here? It looks like you're just replacing the RATES array with the last element of the array. Why do you need two loops for that? Commented Jun 9, 2020 at 16:54

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

he also wants to turn the RATES array into a single object.
I interpreted "I write below code to resolve issue however it take only last value." as that's an issue with the current code. I thought he wanted to keep it as an array.
arr should still be an array, but arr[i].RATES should be an object.
Look at the desired result in the question.
The problem he has is that arr[1].RATES contains the VALUE from the original arr[0].RATES.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.