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

118
Visualizações
Buscar en matriz multidimensional en JAVASCRIPT

Tengo esta matriz con dos objetos y tengo otra matriz con estos ID: [9054,9021,9040,8941,8947] . Quiero filtrar la gran matriz y obtener una nueva matriz con solo ID coincidentes en la segunda matriz.

El problema es obtener la misma configuración de la matriz, solo quiero eliminar las ID que no coinciden. ¿Alguna idea de cómo solucionarlo?

 const data = [ { key: 9054, title: '22', children: [ { key: 8959, title: 'ABR_ADRESSE', idFather: 8959, tableIsFamily: false, children: [] } , { key: 8943, title: 'ABR_CONTACT', idFather: 8943, tableIsFamily: false, children: [] } , { key: 9021, title: 'ABR_POSTE', idFather: 9021, tableIsFamily: false, children: [] } , { key: 8969, title: 'ABR_SOCIETE', idFather: 8969, tableIsFamily: false, children: [] } ] } , { key: 9040, title: '33', children: [ { key: 8957, title: 'THB_Adresse_Famille', idFather: 8957, tableIsFamily: false, children: [] } , { key: 8941, title: 'THB_Contact_Famille', idFather: 8941, tableIsFamily: false, children: [ { key: 8947, title: 'THB_Contact_Table', idFather: 8941 } , { key: 9855, title: 'THB_Contact_Table_BIS', idFather: 8941 } ] } , { key: 8949, title: 'THB_Societe_Famille', idFather: 8949, tableIsFamily: false, children: [] } , { key: 8983, title: 'THB_TELEPHONE', idFather: 8983, tableIsFamily: false, children: [] } , { key: 10070, title: 'THB_TEST_5708', idFather: 10070, tableIsFamily: false, children: [] } ] } ]

La salida esperada debe ser:

 const data = [ { key: 9054, title: '22', children: [ , { key: 9021, title: 'ABR_POSTE', idFather: 9021, tableIsFamily: false, children: [] } ] } , { key: 9040, title: '33', children: [ , { key: 8941, title: 'THB_Contact_Famille', idFather: 8941, tableIsFamily: false, children: [ { key: 8947, title: 'THB_Contact_Table', idFather: 8941 } ] } ] } ]

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

¿Es este el resultado esperado?

 [ {"key": 9054,"title": "22","children": [ {"key": 9021,"title": "ABR_POSTE","idFather": 9021,"tableIsFamily": false,"children": []} ]}, {"key": 9040,"title": "33","children": [ {"key": 8941,"title": "THB_Contact_Famille","idFather": 8941,"tableIsFamily": false,"children": [ {"key": 8947,"title": "THB_Contact_Table","idFather": 8941} ]} ]} ]

Deberá recorrer cada elemento secundario y filtrar recursivamente los elementos secundarios y verificar si la clave secundaria existe dentro de la matriz de claves.

 const data = [ {"key":9054,"title":"22","children":[ {"key":8959,"title":"ABR_ADRESSE","idFather":8959,"tableIsFamily":false,"children":[]}, {"key":8943,"title":"ABR_CONTACT","idFather":8943,"tableIsFamily":false,"children":[]}, {"key":9021,"title":"ABR_POSTE","idFather":9021,"tableIsFamily":false,"children":[]}, {"key":8969,"title":"ABR_SOCIETE","idFather":8969,"tableIsFamily":false,"children":[]}]}, {"key":9040,"title":"33","children":[ {"key":8957,"title":"THB_Adresse_Famille","idFather":8957,"tableIsFamily":false,"children":[]}, {"key":8941,"title":"THB_Contact_Famille","idFather":8941,"tableIsFamily":false,"children":[ {"key":8947,"title":"THB_Contact_Table","idFather":8941}, {"key":9855,"title":"THB_Contact_Table_BIS","idFather":8941} ]}, {"key":8949,"title":"THB_Societe_Famille","idFather":8949,"tableIsFamily":false,"children":[]}, {"key":8983,"title":"THB_TELEPHONE","idFather":8983,"tableIsFamily":false,"children":[]}, {"key":10070,"title":"THB_TEST_5708","idFather":10070,"tableIsFamily":false,"children":[]} ]} ]; // Based on: https://stackoverflow.com/a/41312330/1762224 // For an in-place delete, see: https://stackoverflow.com/a/41312346/1762224 const filterTreeList = (data, opts) => data.filter(item => { if (item[opts.childrenKey]) { item[opts.childrenKey] = filterTreeList(item[opts.childrenKey], opts); } return opts.keys.includes(item[opts.itemKey]); }); const result = filterTreeList(data, { itemKey: 'key', childrenKey: 'children', keys: [9054, 9021, 9040, 8941, 8947] }); console.log(result);
 .as-console-wrapper { top: 0; max-height: 100% !important; }

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