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

100
Vistas
JAVASCRIPT Search in 3D array of objects

I have an array with some data like this :

const
  data = [
    {"key":9054,"title":"1","children":[
      {"key":8959,"title":"1-0","idFather":8959,"tableIsFamily":false,"children":[]},
      {"key":8943,"title":"1-1","idFather":8943,"tableIsFamily":false,"children":[]},
      {"key":9021,"title":"1-2","idFather":9021,"tableIsFamily":false,"children":[]},
      {"key":8969,"title":"1-3","idFather":8969,"tableIsFamily":false,"children":[]}]},
      {"key":9040,"title":"2","children":[
        {"key":8957,"title":"2-0","idFather":8957,"tableIsFamily":false,"children":[]},   
        {"key":8941,"title":"2-1","idFather":8941,"tableIsFamily":false,"children":[
          {"key":8947,"title":"2-1-0","idFather":8941},
          {"key":9855,"title":"2-1-1","idFather":8941}
        ]},
        {"key":8949,"title":"3-0","idFather":8949,"tableIsFamily":false,"children":[]},
        {"key":8983,"title":"3-1","idFather":8983,"tableIsFamily":false,"children":[]},
        {"key":10070,"title":"3-2","idFather":10070,"tableIsFamily":false,"children":[]}
      ]}
    ];

And I have another array with some Id's like this :

[9054, 9021, 9040, 8941, 8947]

I want to filter the data array and get only matched array with the second array, I have tried some map/filter but it seem to be more difficult thann i think, Have you any ideas how to deal with this array please ?

The expected output should be :

const
  data = [
    {"key":9054,"title":"1","children":[
      {"key":9021,"title":"1-2","idFather":9021,"tableIsFamily":false,"children":[]},
      {"key":9040,"title":"2","children":[  
        {"key":8941,"title":"2-1","idFather":8941,"tableIsFamily":false,"children":[
          {"key":8947,"title":"2-1-0","idFather":8941},
        ]},
      ]}
    ];

Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use rectursive function for deep datas. Like this. This function can also be used for deeper structures.

Further information about the recursive function here: https://www.geeksforgeeks.org/recursive-functions/#:~:text=In%20programming%20terms%2C%20a%20recursive,can%20be%20solved%20quite%20easily.

const
  data = [
    {"key":9054,"title":"1","children":[
      {"key":8959,"title":"1-0","idFather":8959,"tableIsFamily":false,"children":[]},
      {"key":8943,"title":"1-1","idFather":8943,"tableIsFamily":false,"children":[]},
      {"key":9021,"title":"1-2","idFather":9021,"tableIsFamily":false,"children":[]},
      {"key":8969,"title":"1-3","idFather":8969,"tableIsFamily":false,"children":[]}]},
    {"key":9040,"title":"2","children":[
        {"key":8957,"title":"2-0","idFather":8957,"tableIsFamily":false,"children":[]},   
        {"key":8941,"title":"2-1","idFather":8941,"tableIsFamily":false,"children":[
          {"key":8947,"title":"2-1-0","idFather":8941},
          {"key":9855,"title":"2-1-1","idFather":8941}
        ]},
        {"key":8949,"title":"3-0","idFather":8949,"tableIsFamily":false,"children":[]},
        {"key":8983,"title":"3-1","idFather":8983,"tableIsFamily":false,"children":[]},
        {"key":10070,"title":"3-2","idFather":10070,"tableIsFamily":false,"children":[]}
      ]}
    ];
 const ids = [9054, 9021, 9040, 8941, 8947]
 
 filterData = (data)=> {
    const newData = data.filter((obj)=>ids.includes(obj.key));
    newData.forEach((obj)=>{obj.children ? obj.children = filterData(obj.children) : []});
    
    return newData;
 }
 
 console.log(filterData(data));

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