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

122
Views
How do i filter a value of key in an object based on matching value?

I have an object that contains objects. How do i filter out the value of "title" based on whether the id is matching?

{
    "0": {
        "id": 1912,
        "title": "Fiction",
    },
    "1": {
        "id": 1957,
        "title": "Non-Fiction",
    },
    "2": {
        "id": 1958,
        "title": "Literature",
    },
}

For example, if the const id=1958, then the output: Literature

Output:

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

0

You can try this:

let data = {
    "0": {
        "id": 1912,
        "title": "Fiction",
    },
    "1": {
        "id": 1957,
        "title": "Non-Fiction",
    },
    "2": {
        "id": 1958,
        "title": "Literature",
    },
};

let getTitle = (obj, id)=>{
 return Object.values(obj).filter(x=>x.id===id)?.[0]?.title
};

console.log(getTitle(data,1958))

You can also use find instead of filter.

Without optional chaining as per OPs comment:

   let data = {
        "0": {
            "id": 1912,
            "title": "Fiction",
        },
        "1": {
            "id": 1957,
            "title": "Non-Fiction",
        },
        "2": {
            "id": 1958,
            "title": "Literature",
        },
    };

    let getTitle = (obj, id)=>{
      let element = Object.values(obj).find(x=>x.id===id);
      return element ? element.title : "";
    };

    console.log(getTitle(data,1958))

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!