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

86
Views
how to filter object from nested objects by specific unique key

This is input data, where i want filter a object by ID key

let myData = {
             "nodeInfo":
             {
                "9":
                {
                   "1": { "ID": "14835", "name": "Binod" },
                   "2": { "ID": "14836", "name": "Rahul" },
                   "3": { "ID": "14837", "name": "Sunil" },
                },
                "10":
                {
                   "4": { "ID": "14839", "name": "Shikhar" },
                   "5": { "ID": "14840", "name": "Hari" },
                   "6": { "ID": "14841", "name": "Harsh" },
                }
             }
          };

i want which object who have ID value 14835 so my result would be:: { "ID": "14835", "name": "Binod" }

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

0

Using an arrow function expression, what about

getter = ID => Object.values(
    myData.nodeInfo
).map(
    o => Object.values(o).filter(
        v => v.ID === ID
    )[0]
)

and then

> getter("14835")
< (2) [{…}, {…}]

And, depending on whether your identifiers are unique or not, you can actually do

> getter("14835")[0]
< {ID: '14835', name: 'Binod'}
about 4 years ago · Juan Pablo Isaza Report

0

This supports many levels of nested objects

function isObject(possibleObject) {
  return typeof possibleObject === 'object' && possibleObject !== null
}

function find(data, key) {
  for (const element of Object.values(data)) {
    if (element.ID) {
      if (element.ID === key) {
        return element;
      }

      continue;
    }

    if (isObject(element)) {
      const foundElement = find(element, key);
      if (foundElement !== null) {
        return foundElement;
      }
    }
  }

  // Not found
  return null;
}
``
about 4 years ago · Juan Pablo Isaza Report

0

@Gluey1017 has provided a very good answer that will return the first occurrence of an object with the given ID key. In the following snippet I modified/extended his script to also collect multiple results should they exist:

const myData = {
         "nodeInfo":
         {
            "9":
            {
               "1": { "ID": "14835", "name": "Binod" },
               "2": { "ID": "14836", "name": "Rahul" },
               "3": { "ID": "14837", "name": "Sunil" },
            },
            "10":
            {
               "4": { "ID": "14839", "name": "Shikhar" },
               "5": { "ID": "14840", "name": "Hari" },
               "6": { "ID": "14841", "name": "Harsh" },
            },
            "15":
            { "7": {
                "8": { "ID": "14835" , "name": "Carsten" },
                "9": { "ID": "14842" , "name": "someone" }
            } }
         }
      };
function find(data,key){
 const found=[]; // results array
 function fnd(da) { // recursive function
  for (const el of Object.values(da)){
   if (el?.ID === key) found.push(el);
   if (typeof el==='object' && el !== null) fnd(el);
  }
 }
 fnd(data);
 return found;
}

console.log(find(myData,"14835"));

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!