0

I just want to increase the quantity for all my items in the aray. How can I reach that? Thank you!

const products = [
  { name: 'birou', quantity: 23, type: 'furniture'},
  { name: 'masa', quantity: 44, type: 'furniture'},
  { name: 'scaun', quantity: 65, type: 'furniture' },
  { name: 'covor', quantity: 133, type: 'decoration' },
  { name: 'candelabru', quantity: 64, type: 'decoration' },
  { name: 'perdea', quantity: 215, type: 'decoration' },
  { name: 'draperie', quantity: 94, type: 'decoration' },
  { name: 'ceas perete', quantity: 32, type: 'decoration' },
  { name: 'tablou', quantity: 71, type: 'decoration' },
  { name: 'parchet', quantity: 145, type: 'furniture' },
  { name: 'pres', quantity: 21, type: 'decoration' },
  { name: 'bibelou', quantity: 14, type: 'decoration' },
];


products.forEach(item => {
  this.quantity += 120;
  return item;
})
isherwood
  • 58,414
  • 16
  • 114
  • 157
Maolean
  • 9
  • 3

2 Answers2

1
products.forEach(item => {
  this.quantity += 120;
//^^^^ you need to use item instead of this
  return item;
//^^^^^^^^^^^^ you don't need to return anything.
})

Based on the style of what you posted, you might be confusing the use of Array.forEach with Array.map, and you need to make sure you are understanding 'pass by reference' semantics when working with arrays and objects.

Your code will work in a forEach by virtue of item being passed by reference. The return value is of no consequence, so can be omitted. I think this use of forEach is risky as its a non-obvious side effect that can go unnoticed.

However, if you were using map, you'd end up modifying the original object for each element, merely creating a new Array that contains references to the objects contained in the source array. In essense, the source array would contain identical objects to the destination array, and you'd be repeating the same non-obvious side effect. For map, you need to return a completely new object (and be careful when destructuring the existing properties into it if the object contains non-scalar values, which will be 'by reference' to the originals).

const modifiedArray = products.map(item => {
  return {
    ...item,  // assumes no non-scalar properties
    quantity: item.quantity + 128
  }
})
Dave Meehan
  • 3,133
  • 1
  • 17
  • 24
1

You can also use for ... of as follows:

const products = [
  { name: 'birou', quantity: 23, type: 'furniture'},
  { name: 'masa', quantity: 44, type: 'furniture'},
  { name: 'scaun', quantity: 65, type: 'furniture' },
  { name: 'covor', quantity: 133, type: 'decoration' },
  { name: 'candelabru', quantity: 64, type: 'decoration' },
  { name: 'perdea', quantity: 215, type: 'decoration' },
  { name: 'draperie', quantity: 94, type: 'decoration' },
  { name: 'ceas perete', quantity: 32, type: 'decoration' },
  { name: 'tablou', quantity: 71, type: 'decoration' },
  { name: 'parchet', quantity: 145, type: 'furniture' },
  { name: 'pres', quantity: 21, type: 'decoration' },
  { name: 'bibelou', quantity: 14, type: 'decoration' },
];

for(const item of products) {
    item.quantity += 120;
}
      
console.log( products );
PeterKA
  • 24,158
  • 5
  • 26
  • 48