1

So i have a similar array of objects like this one:

this.array = [{apple: 2, oranges: 100}, {apple: 4, oranges: 120}, {apple: 6, oranges: 110}];

I want to iterate through the array and update the values of oranges to have the same values as apple. So i would have the following result:

this.array = [{apple: 2, oranges: 2}, {apple: 4, oranges: 4}, {apple: 6, oranges: 6}];

what is a good way to get that result?

I tried to do the following but did not work

this.array = this.array.forEach(fruit => fruit.oranges === fruit.apple);

grmnth
  • 61
  • 2
  • 8
  • 2
    `fruit.oranges === fruit.apple` tests for strict equality - you want `fruit.oranges = fruit.apple` - and you also DONT want `this.array = ` ... since `array.forEach` returns undefined – Jaromanda X Jun 16 '21 at 10:16
  • @adiga - yep, there's a difference between using .map and .forEach ... it may be an important one ... code in question mutates the contents of this.array ... .map replaces this.array with a new array ... completely different semantics – Jaromanda X Jun 16 '21 at 10:33
  • @JaromandaX I know about that. But, that doesn't answer OP's simple question. Not everyone wants to create a new array all the time. – adiga Jun 16 '21 at 10:34
  • @adiga - I was agreeing with you :p – Jaromanda X Jun 16 '21 at 10:35

2 Answers2

1

Let's understand your code:

array.forEach(fruit => fruit.oranges === fruit.apple);

Let's expand the callback function:

array.forEach(fruit => {
  return fruit.oranges === fruit.apple;
});

The forEach method should be used only when you want to iterate over each element of the array. It won't do anything with the value you return from the given callback.

You could, however, modify the === operator (which shall be used to compare two values) to use the = operator to reassign the value of the original fruit's orange property.

Like so:

const arr = [
  { apple: 2, oranges: 100 },
  { apple: 4, oranges: 120 },
  { apple: 6, oranges: 110 }
];

arr.forEach((fruit) => {
  // Reassign the `oranges` property with the `apple` value:
  fruit.oranges = fruit.apple
});

console.log(arr);

Notice that forEach does not return the array. In this case, we are just iterating over each element and modifying each element's oranges property.

Another option, when you do NOT want to modify the original values is to use Array.prototype.map instead of Array.prototype.forEach.

Check out this question to find out the differences between map and forEach.

Something like:

const arr = [
  { apple: 2, oranges: 100 },
  { apple: 4, oranges: 120 },
  { apple: 6, oranges: 110 }
];

const newArr = arr.map((obj) => ({
  // Forward the apple property:
  apple: obj.apple,
  // Assign a new value to the oranges property:
  oranges: obj.apple
}));

console.log(newArr);

I must add that with that code, you will not, in fact, modify the array nor its original objects. You are instead creating a new array (returned by map) with new objects.

Luiz Felipe
  • 869
  • 12
  • 21
0

Better map the array instead of using foreach

this.array = [{apple: 2, oranges: 100}, {apple: 4, oranges: 120}, {apple: 6, oranges: 110}];
this.array = this.array.map(fruit => ({...fruit, oranges : fruit.apple }));
console.log(this.array)
p2pdops
  • 505
  • 4
  • 11