Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda