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

222
Views
find object if attribute not exists and add that attribute with value

I have array of objects like below:

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"
    }
  ];

In above array of objects, I need to add rank=0 to the objects where rank is not present.

I tried like below, but not able to figure it out correct way of doing this.

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

What is right way of doing this? Please help and guide. Thanks

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

0

I would just do:

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

Since you spread item after rank, if it has a rank the 0 will be overwritten, if it doesn't exist, it will end up with 0 for the rank.

If performance is a concern, it is probably more efficient to do:

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!