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

116
Vistas
Search in multi dimension Array in JAVASCRIPT

I have this array with two object, and I have another array with these IDs: [9054,9021,9040,8941,8947]. I want to filter the big array and get a new array with only matched IDs on the second array.

The problem is to get the same configuration of the array, I want just to delete the unmatched IDs. Any idea how to solve it?

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: [] } 
  ] } ] 

The expected Output should be :

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

0

Is this your expected output?

[
  {"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}
    ]}
  ]}
]

You will need to loop over each child and recursively filter the children and check if the sub-child key exists within the keys array.

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 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