-1

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!

3
  • Hi, can you post an example of the expected output? Commented Jan 28, 2022 at 21:30
  • Hi, done! :) please take a look in the edited post :) Commented Jan 28, 2022 at 21:35
  • You asked this question 4 hours ago, rather than reposting the same question you should edit your original question so that it gets the answers you need – clarify any details asked for in the comments, and show what you have tried. see How do I get attention for one of my own questions without a good answer? Commented Jan 28, 2022 at 21:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.