1

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 ?

8
  • I reformatted you question so it is readable and made a snippet. but the snippet gives console errors Commented Apr 15, 2022 at 10:21
  • Thank your for your formatting, yes sometimes it show an error, I still don't know why, When I'm triyng to find in myDatas array I get this error Commented Apr 15, 2022 at 10:25
  • Change to let item = table.myDatas?.find((el) => el.ID === id); since you do not have myDatas in the last entry Commented Apr 15, 2022 at 10:28
  • I edited my array, now it work's for my example, I want to know if there is another best solutions to refactoration now :) Commented Apr 15, 2022 at 10:31
  • So you are saying that your code works but you are looking for a better solution? Commented Apr 15, 2022 at 10:34

2 Answers 2

1

I think you need to solve this problem in two steps:

  • First, create a flat array of tables
  • Then filter the array by conditions

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"}]}]}];

const wantedData = [1235, 98523, 97766];

const flatTables = bigArray.flatMap(({ Tables }) => 
  Tables.flatMap(({ myDatas, TableId, TableName }) => 
    myDatas.map((data) => ({ ...data, TableId, TableName })) ));

const result = flatTables.filter(({ ID }) => wantedData.includes(ID));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

I think if you do it something like this you'll be able to map after you've found the right things rather than mapping everything and then filtering that result:

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', }, ], }, ], }, ];

const wantedData = [1235, 98523, 97766];

const wanted_set = new Set(wantedData);

const push_concat = (arr1, arr2) => {
  for(let i = 0; i < arr2.length; i++)
    arr1.push(arr2[i]);
  
  return arr1;
};

const res = bigArray.reduce(
  (acc, { Tables }) =>
    push_concat(acc, 
      Tables.flatMap(({ TableId, TableName, myDatas }) =>
        myDatas
            .filter(({ ID }) => wanted_set.has(ID))
            .map(({ ID, title }) => ({ ID, title, TableId, TableName }))
      )
    ),
  []
);


console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.