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

72
Vistas
Why is this js obj changing depending on how I acces its information?

So I'm trying to modify an object I receive via api with more info, like so:

useEffect(async () => {
setPedidos(await getPedidos()); //Get initial data from api
 }, []);
useEffect(async () => { //gets triggered when 'pedidos' is modified
if (pedidos.length > 0) {
  console.log('example from original obj:', pedidos[0])
  let obj = [...pedidos] //copies contents of 'pedidos' to a new list
  pedidos.forEach(async (item, index) => {
    obj[index].cliente = await getCliente(obj[index].cliente) //updates 'cliente' 
  })
  console.log('log 1', obj) //logs wrole obj. Shows updated info as expected
  console.log('log 2', obj[0]) //Logs first in set. Shows original info
}
}, [pedidos]);

Logs When I later try to access new information, I get the message 'cannot read properties of undefined'.

I haven't really tried much since I have no idea what to do. There is no reason for the new object to change at all since 'let obj = [...pedidos]' makes a copy of the original object, as far as I understand.

about 4 years ago · Juan Pablo Isaza
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