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

227
Views
encuentre el objeto si el atributo no existe y agregue ese atributo con valor

Tengo una variedad de objetos como a continuación:

 checklist= [ { "participantId": 13, "rankStatus": "participated", "rank": 3, "comment": "my comment", "horse_name": "test232 fdfgdg", "country": "Afghanistan", "life_number": null }, { "participantId": 12, "rankStatus": "eliminated", "comment": null, "horse_name": "test horse", "country": "Algeria", "life_number": "234234" }, { "participantId": 11, "rankStatus": null, "rank": null, "comment": null, "horse_name": "tesdfs", "country": "Afghanistan", "life_number": null }, { "participantId": 10, "rankStatus": null, "comment": null, "horse_name": "nam horse", "country": "India", "life_number": "fh345" } ];

En la matriz de objetos anterior, necesito agregar rank=0 a los objetos donde el rank no está presente.

Intenté como se muestra a continuación, pero no pude encontrar la forma correcta de hacerlo.

 checklist.filter(x => !x.hasOwnProperty('rank') ).map(x => x.rank == 0);

¿Cuál es la forma correcta de hacer esto? Por favor ayuda y guía. Gracias

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

0

Yo solo haría:

 checklist.map(item => ({ rank: 0, ...item }))

Dado que distribuye item tras rango, si tiene un rank , el 0 se sobrescribirá, si no existe, terminará con 0 para el rango.

Si el rendimiento es una preocupación, probablemente sea más eficiente hacer:

 checklist.forEach((item) => item.rank = item.rank || 0);
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!