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

67
Views
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
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!