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

107
Vistas
update Object value globally

I want to update the object value of shoppingItems
using the es6 spread operator updateObj updates the Object but only inside the function
why does the Object inside shoppingItems does not change its value ?

const kontorGoods = [{ cheese: 5.5 }, { rice: 2.5 }, { meat: 8.8 }];
const shoppingItems = [{ cheese: 5.5 }];


function updateValue(itemIndex){
    const item = Object.values(kontorGoods[itemIndex])[0];
    updateObj(shoppingItems, itemIndex);
    console.log(updateObj(shoppingItems, itemIndex))
}


function updateObj(objArray, objIndex) {
    const currentObjkey = Object.keys(objArray[objIndex])[0];
    const currentObjValue = Object.values(objArray[objIndex])[0];
  
    return {
      ...objArray[objIndex],
      // add the key as a variable
      [currentObjkey]: currentObjValue + currentObjValue,
    };
  }

updateValue(0)
console.log(shoppingItems)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

An object literal creates a new object, it doesn't have any effect at all on the things that you're using to fill in its properties, including any objects you use spread syntax on.

To update the object in the array, write back to the array:

    objArray[objIndex] = {
//  ^^^^^^^^^^^^^^^^^^^^^
      ...objArray[objIndex],
      // add the key as a variable
      [currentObjkey]: currentObjValue + currentObjValue,
    };
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