'JAVASCRIPT Search in 3D array of objects [duplicate]

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!



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1