I want to search in a big array for different id from another array and print all intersections of those two arrays
I want to map through my bigTable and I want to create another array of correspondence, each found element must contain all fields+tableName+tableID like this :
const output = [{ ID: 1234, title: 'title1', TableName: 'loramIpsum', TableId: 11, }, { ID: 98523, title: 'mylasttitle', TableName: 'table2', TableId: 87545, }, { ID: 97766, title: 'mylastdata', TableName: 'table2', TableId: 87545, }, ] I've create a function but I think there is another best and sample solution, this is my function :
const getResult = (wantedData, bigArray) => { return wantedData.flatMap((id) => bigArray.flatMap((family) => family.Tables.flatMap((table) => { let item = table.myDatas.find((el) => el.ID === id); if (item) { item.Table = table.TableName; item.familyId = family.GridId; return item; } }).filter((result) => result !== undefined) ) ); }; console.log(getResult(wantedData, bigArray)) <script> const wantedData = [1235, 98523, 97766]; const bigArray = [{ bigArrayId: 1111, Tables: [{ TableId: 11, TableName: 'loramIpsum', myDatas: [{ ID: 1234, title: 'title1', }, { ID: 1235, title: 'title2', }, ], }, ], }, { bigArrayId: 674665, Tables: [{ TableId: 87545, TableName: 'table2', myDatas: [{ ID: 98523, title: 'mylasttitle', }, { ID: 24134, title: 'alex', }, { ID: 97766, title: 'mylastdata', }, ], }, ], }, ]; </script> Any help please ? Can I do it with recursive function ?
let item = table.myDatas?.find((el) => el.ID === id);since you do not have myDatas in the last entry