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

113
Visualizações
how to filter nested objects based on properties

Below is a nested array of objects, how can I filter this based on object property? ex. if name==="same" then it should return that object

const arrNestedObj=[
          {
            "id":"1",
            0:{
              "name":"john",
              "age":"10"
            },
            1:{
              "name":"sam",
              "age":"20"
            }
          },{ 
            "id":"2",
            0:{
              "name":"sam",
              "age":"15"
            },
          }
        ]
expected output:
if name==="same" then it should return that object
[
  {
    "name":"sam",
    "age":"20"
  },
  {
     "name":"sam",
     "age":"15"
   }
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If your meaning is that you want the name === 'sam' object, then can use Array.prototype.map() and Object.getOwnPropertyNames
These two method can help you to get the objects.

example:

<script>
const arrNestedObj=[
          {
            "id":"1",
            0:{
              "name":"john",
              "age":"10"
            },
            1:{
              "name":"sam",
              "age":"20"
            }
          },{ 
            "id":"2",
            0:{
              "name":"sam",
              "age":"15"
            },
          }
        ]

let result = [];
arrNestedObj.map((item) => {
  let tempArray = Object.getOwnPropertyNames(item)
    for(let i=0; i<tempArray.length-1; i++)
    {
     if(Number(tempArray[i] !== NaN))
     {
       if(item[tempArray[i]].name === "sam")
       {
        result.push(item[tempArray[i]]);
       }
     }
    }
});

console.log(result)
</script>

about 4 years ago · Juan Pablo Isaza Relatório

0

Use the Array.prototype.filter method.

const filteredArr = arrNestedObj.filter(elem => elem.name === "sam")
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is my solution

const output = data.map(i=>Object.values(i).filter(v=>v.name==="sam")).reduce((holder, cur)=>{
    return [...holder,...cur]
},[])
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