2 回答

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
如果id屬性是唯一的,您可以用它過(guò)濾掉。
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貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
如果需要在對(duì)象之間進(jìn)行深度相等檢查,那么我們可以使用 lodash 與andisEqual一起進(jìn)行深度相等檢查: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>
添加回答
舉報(bào)