【发布时间】:2021-06-19 07:23:49
【问题描述】:
我有两个对象:
对象一
[
{ repo: 'edat-ims-github', status: 200 },
{ repo: 'edat-ims-github-spa', status: 200 },
{ repo: 'test-repo-three', status: 200 }
]
对象二
[
{ repo: 'edat-ims-github', status: 200 },
{ repo: 'edat-ims-github-spa', status: 200 },
{ repo: 'test-repo-one', status: 200 },
{ repo: 'test-repo-two', status: 200 },
{ repo: 'test-repo-three', status: 200 }
]
我想比较两个数组并从第二个数组中删除重复的对象,因此我的输出如下所示:
[
{ repo: 'test-repo-one', status: 200 },
{ repo: 'test-repo-two', status: 200 }
]
我尝试使用 ES6 来执行此操作:
const result = objectTwo.filter((obj) => {
return !objectOne.includes(obj);
});
但是,结果的结果是:
[
{ repo: 'edat-ims-github', status: 200 },
{ repo: 'edat-ims-github-spa', status: 200 },
{ repo: 'test-repo-one', status: 200 },
{ repo: 'test-repo-two', status: 200 },
{ repo: 'test-repo-three', status: 200 }
]
有人可以指导我哪里出了问题以及实现这一目标的最佳方法是什么?在现实生活中,两个数组都有 10000 多个对象。
我没有测试相等性,因为两个数组不一样,我更多的是测试如何删除重复项。
谢谢:)
【问题讨论】:
标签: javascript node.js loops ecmascript-6