2 回答
TA贡献1812条经验 获得超5个赞
如果id属性是唯一的,您可以用它过滤掉。
let arr1 = [
{ id: "1", txt: "first"},
{ id: "2", txt: "second"},
{ id: "3", txt: "third"}
]
let arr2 = [
{ id: "2", txt: "second"},
]
let arr3 = arr1.filter(e1 => !arr2.some(e2 => e2.id === e1.id));
console.log(arr3);
TA贡献1893条经验 获得超10个赞
如果需要在对象之间进行深度相等检查,那么我们可以使用 lodash 与andisEqual一起进行深度相等检查:Array#filterArray#some
let arr1 = [
{ id: "1", txt: "first"},
{ id: "2", txt: "second"},
{ id: "3", txt: "third"}
]
let arr2 = [
{ id: "2", txt: "second"},
]
const filterArr = (arr1, arr2) =>{
return arr1.filter(a1 => !arr2.some(a2 => _.isEqual(a1, a2)))
}
console.log(filterArr(arr1, arr2))
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
添加回答
举报