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

180
Views
JS empuja el objeto a la matriz por valor

¿Cómo inserto un objeto en una matriz por valor en lugar de por referencia? Específicamente, quiero mutar un objeto en un bucle for e impulsar su valor en cada iteración. Si hay una mejor manera de hacer esto sin mutar el objeto, esa también funciona.

Aquí está el problema:

 > let foo = {"a": 1}; undefined > let arr = []; undefined > for (let i=0;i<10;i++) { ... arr.push(foo); ... foo.a += 1; ... } 11 > arr [ { a: 11 }, { a: 11 }, { a: 11 }, { a: 11 }, { a: 11 }, { a: 11 }, { a: 11 }, { a: 11 }, { a: 11 }, { a: 11 } ]

yo lo que quiero es arr tener

 [ { a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }, { a: 6 }, { a: 7 }, { a: 8 }, { a: 9 }, { a: 10 } ]

(ignore el error off-by-1)

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!