数组去重
let submitData = [
{ id: 1, name: "122" },
{ id: 2, name: "2" },
{ id: 3, name: "3" },
{ id: 3, name: "3" },
]
let filterSubmitData = [];
submitData.map(item=>{
let flag = filterSubmitData.some(element=>{
return item.id === element.id
})
if ( !flag ) {
filterSubmitData.push(item)
}
})
console.log(filterSubmitData)
数组对象的交集
let oneData = [
{ id: 1, name: "122" },
{ id: 2, name: "2" },
{ id: 3, name: "3" },
{ id: 4, name: "4" },
]
let twoData = [
{ id: 4, name: "122" },
{ id: 5, name: "2" },
{ id: 6, name: "3" },
{ id: 3, name: "3" },
]
let resultdata = [];
oneData.map(item=>{
let flag = twoData.some(element=>{
return element.id === item.id;
})
if( flag ) {
resultdata.push(item)
}
})
console.log( resultdata )
数组的差集
解决思路: 先找到两个对象数组的唯一标识的交集A,然后两个原数组的并集中去掉这个A
let oneData = [
{ id: 1, name: "122" },
{ id: 2, name: "2" },
{ id: 3, name: "3" },
{ id: 4, name: "4" },
]
let twoData = [
{ id: 3, name: "3" },
{ id: 4, name: "122" },
{ id: 5, name: "2" },
{ id: 6, name: "3" },
]
let testData = [];
oneData.map(item=>{
let flag = twoData.some(element=>{
return ( element.id === item.id )
})
if (flag) {
testData.push(item.id)
}
})
let allData = [...oneData, ...twoData];
let newData = allData.filter(item=>{
return testData.indexOf(item.id) === -1
})
console.log(newData)