Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
How can I increase the value of a property in an array of objects?

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;
})
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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
  }
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!