javascript 中两个数组的交集

Intersection of two arrays in javascript

我想在一个大数组中搜索与另一个数组不同的 id 并打印这两个数组的所有交集

我想通过我的 bigTable 进行映射,我想创建另一个对应数组,每个找到的元素必须包含所有字段+tableName+tableID,如下所示:

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

我已经创建了一个函数,但我认为还有另一个最佳示例解决方案,这是我的函数:

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>

有什么帮助吗?我可以用递归函数来做吗?

我认为你需要分两步解决这个问题:

  • 首先,创建一个表的平面数组
  • 然后按条件筛选数组

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

我认为如果你这样做的话,你将能够在找到正确的东西之后进行映射,而不是映射所有内容然后过滤结果:

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