【发布时间】:2020-10-16 17:40:42
【问题描述】:
我有两个数组对象,我想比较它们,如果两个数组中的值匹配则删除。
Arr1 = [
{id: "one", name: "one", status: "Active"},
{id: "two", name: "two", status: "Active"},
{id: "three", name: "three", status: "Active"}
]
Arr2 = [
{pid: "one", order: 1},
{pid: "two", order: 2}
]
现在我想如果id === pid 那么它不应该返回。
finalArr = [
{id: "three", name: "three", status: "Active"}
]
我尝试了以下解决方案,但不知何故它不起作用并返回空数组。
finalArr = Arr1.filter((a) => {
Arr2.some((b) => {
a['id'] === b['pid'];
});
});
如果您需要更多信息,请告诉我。我希望我能够表达我的担忧。4
谢谢!
【问题讨论】:
-
some应该返回一些东西。使用括号 ({) 会强制您添加return,否则会隐式返回void 0(undefined)。
标签: javascript arrays typescript ecmascript-6 lodash