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

134
Views
Cómo encontrar y reemplazar el elemento de la matriz de valores si la condición es verdadera

Tengo una matriz como la siguiente

 const arr = [ { id: "first", val: 'ganguly', }, { id: "third", val: 'sachin', }, ] const selectedVaue ='dhoni';

si la identificación coincide con 'tercero', entonces valor para reemplazar a clave particular

 const list = arr.filter(data => data.id === 'third'); if (list.length > 0 ) { // code }

El resultado esperado se encuentra a continuación:

 const arr = [ { id: "first", val: 'ganguly', }, { id: "third", val: 'dhoni', }, ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar Array.prototype.map y crear una nueva matriz con el valor con id "third" reemplazado.

 const arr = [ { id: "first", val: "ganguly" }, { id: "third", val: "sachin" }, ]; const selectedValue = "dhoni"; const res = arr.map((a) => a.id === "third" ? { ...a, val: selectedValue } : a ); console.log(res);

O puede usar Array.prototype.forEach si desea reemplazar el objeto en su lugar.

 const arr = [ { id: "first", val: "ganguly" }, { id: "third", val: "sachin" }, ]; const selectedValue = "dhoni"; arr.forEach((a, i) => { if (a.id === "third") { arr[i] = { ...a, val: selectedValue }; } }); console.log(arr);

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!