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

80
Views
Filter an Object and Return Key in Typescript

I have an object to be filtered and it should return a key that has a specific id. Id is Unique . Need an efficient logic to return this expected output.The Object to be filtered.

{
  "a":[{"id":"1123","value":"test1"}],
  "b":[{"id":"1124","value":"test2"}],
  "c":[{"id":"1125","value":"test3"}]
  
}

Input Id: "1124"

Expected Output  : 'b'
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

let data  = {
  "a":[{"id":"1123","value":"test1"}],
  "b":[{"id":"1124","value":"test2"}],
  "c":[{"id":"1125","value":"test3"}]
};

let input = "1124";

let result  = Object.entries(data).filter(([k,v]) => v[0].id === input)[0][0];
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Efficiencies here:

  • break the loop as soon as something is found
  • not interested in the object that has the id, only in checking that something there has that id

o = {
  "a":[{"id":"1123","value":"test1"}],
  "b":[{"id":"1124","value":"test2"}],
  "c":[{"id":"1125","value":"test3"}]
  
}
for (key in o) {
  if (o[key].some(x => x.id === '1124')) {
    console.log(key);
    break;
  }
}

about 4 years ago · Juan Pablo Isaza Report

0

const input = "1124"

const obj = {
  "a":[{"id":"1123","value":"test1"}],
  "b":[{"id":"1124","value":"test2"}],
  "c":[{"id":"1125","value":"test3"}]  
}

Object.values(obj).filter((ob, i)=>{if(ob[0].id === input){console.log(Object.keys(obj)[i])}})

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!