Home > Software engineering >  how to filter object from nested objects by specific unique key
how to filter object from nested objects by specific unique key

Time:04-16

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

CodePudding user response:

Using an arrow function expression, what about

getter = ID => Object.keys(
    myData.nodeInfo
).map(
    k => myData.nodeInfo[k]
).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'}

CodePudding user response:

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;
}
``

CodePudding user response:

@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"));

CodePudding user response:

Using spread operator to find item from a flat array of objects:

// get nodeInfo values
Object.values(myData.nodeInfo)
  // reduce to flat array of objects
  .reduce((acc,v) => [...acc, ...Object.values(v)],[])
  // assuming ID is unique find object
  .find(o => o.ID === "14835")
  • Related