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

177
Views
Extract the last item which matches the condition

I have an array of objects, where I am checking for a boolean property, I need to get the last one from the array of objects which is true and need to get the name property.

This is what I have done, is there any better approach or simple way to achieve this:

const a = [{is_done: true, name: 'a'}, {is_done: true, name: 'b'}, {is_done: false, name: 'c'}]


let output = a.filter(a => a.is_done).slice(-1)[0].name


console.log(output)

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

0

You can achieve this by first applying reverse and then using find which will return the first item which corresponds to the condition you specify.

Please note that you would probably want to apply the reverse method on a copy of a string and not on the original so it's better to use [...a] as hjrshng commented below.

const a = [{is_done: true, name: 'a'}, {is_done: true, name: 'b'}, {is_done: false, name: 'c'}]

const res = [...a].reverse().find(x => x.is_done);

console.log(res.name)

Please also note that in the near future, you will be able to use findLast and findLastIndex which are currently in stage 3 - https://github.com/tc39/proposals

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!