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

176
Visualizações
Filtering on nested array of objects to return the entire matching object

My array of nested objects looks like this along with the array to match it with

let findThis = ["Water"];
    let arrayOfElements = 
    [
            {
               "code": "a",
               "templates": [
                 {
                   "templateCode": "Water",
                   "title": "Earth"
                 },
                 {
                   "templateCode": "Milk",
                   "title": "Sky"
                 }
                 
                ]
            },
            {
               "code": "b",
               "templates": []
            },
            {
               "code": "c",
               "templates": [
                 {
                   "templateCode": "Water",
                   "title": "Earth"
                 },
                 {
                   "templateCode": "Tree",
                   "title": "Moon"
                 }
            },
            {
               "code": "d",
               "templates": [
                 {
                   "templateCode": "Tooth",
                   "title": "Tiger"
                 }
            }
    ]

I want to extract those objects whose templateCode is Water. So my final returned array should look like this.

let result = 
[
        {
           "code": "a",
           "templates": [
             {
               "templateCode": "Water",
               "title": "Earth"
             },
             {
               "templateCode": "Milk",
               "title": "Sky"
             }
             
            ]
        },
        {
           "code": "c",
           "templates": [
             {
               "templateCode": "Water",
               "title": "Earth"
             },
             {
               "templateCode": "Tree",
               "title": "Moon"
             }
        }
]

I tried:

arrayOfElements.filter(x => x.templates.filter(y => findThis.includes(y.templateCode)));

But it is returning the entire array itself and is not working.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You need to filter the arrayOfElements with the condition of any templateCode existing inside the findThis array.

let findThis = ["Water"];
    let arrayOfElements = 
    [
            {
               "code": "a",
               "templates": [
                 {
                   "templateCode": "Water",
                   "title": "Earth"
                 },
                 {
                   "templateCode": "Milk",
                   "title": "Sky"
                 }
                 
                ]
            },
            {
               "code": "b",
               "templates": []
            },
            {
               "code": "c",
               "templates": [
                 {
                   "templateCode": "Water",
                   "title": "Earth"
                 },
                 {
                   "templateCode": "Tree",
                   "title": "Moon"
                 }
                 ]
            },
            {
               "code": "d",
               "templates": [
                 {
                   "templateCode": "Tooth",
                   "title": "Tiger"
                 }
                 ]
            }
    ]
    
const result = arrayOfElements.filter(item => item.templates.some(t => findThis.includes(t.templateCode)))

console.log(result)

about 4 years ago · Santiago Gelvez 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