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

240
Views
Cómo insertar un objeto en un índice específico. el empalme no me funciona

Intenté reemplazar mis elementos específicamente con un objeto. Pero solo se suma como un empujón y hace que la matriz sea más larga.

 let arr = []; function addToArray(index){ let comment = value; let score= scoreType; let scorenow = score; let id = id; arr.splice(index,0,{comment,score,scorenow ,id }); }

Estoy descubriendo una forma de insertar este objeto solo en el índice de arr.

Ejemplo: addToArray(2) debería devolver un [undefined,undefined,{comment,score,scorenow,id}]

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puedes usar esta sintaxis:

 let arr = []; function addToArray(index){ let comment = 1; let score= 2; let scorenow = 3; let id = 4; arr[index] = { comment, score, scorenow, id }; } addToArray(2); console.log(arr);

Si desea hacerlo con el método de empalme, primero debe definir la longitud del arreglo:

 let arr = []; function addToArray(index){ let comment = 1; let score= 2; let scorenow = 3; let id = 4; arr.length = index; arr.splice(index, 0, { comment, score, scorenow, id }); } addToArray(2); console.log(arr);

about 4 years ago · Juan Pablo Isaza Report

0

prueba esto en su lugar:

 arr.splice(index,1,{comment,score,scorenow ,id });

poner 1 en lugar de 0 reemplazará el elemento específico, no agregará uno nuevo

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