【发布时间】:2020-05-04 23:38:27
【问题描述】:
我有两个具有相同元素但顺序不同的二维数组。我想过滤一个考虑到它是否已经存在于第二个数组中。 两个数组的例子:
const firstArray = [['45614726','2020-4-28'],['45610125','2020-4-28'],['45880944','2020-4-28'],['43452341','2020-4-28']] // there are like 40 arrays inside, not sorted
const secondArray = [['34347896', '2020´4-30'],['45614726','2020-4-28'],['45610125','2020-4-28'],['45880944','2020-4-28'],['45892916','2020-4-28']] // there are like 300 arrays inside, not sorted
我想消除“firstArray”中第一个索引重复的“secondArray”数组。
secondArray =[['34347896', '2020´4-30'], ['45892916','2020-4-28']]
我尝试了几件事,我知道最有用的操作是使用 .reduce 但似乎我无法使其工作。
const notPosted = secondArray.reduce((a, b) => {
if (!firstArray[a[0]]) a.different.push(b);
return a;
}, {different: []});
谢谢!
【问题讨论】:
-
你必须在数组中查找,使用array.find
标签: javascript arrays node.js