Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda