Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda