js 两个数组进行去重处理,返回去重后的数组
1.去重的方法为:
array_diff(a, b) { for (var i = 0; i < b.length; i++) { for (var j = 0; j < a.length; j++) { if (a[j].id == b[i].id) { a.splice(j, 1); j = j - 1; } } } return a; }
2.测试方法:
function array_diff(a, b) { for (var i = 0; i < b.length; i++) { for (var j = 0; j < a.length; j++) { if (a[j].id == b[i].id) { a.splice(j, 1); j = j - 1; } } } return a; } var arr1=[ {name:"父节点1",id:1}, {name:"父节点2",id:2}, {name:"父节点3",id:3}, {name:"父节点4",id:4}, ]; var arr2=[ {name:"父节点1",id:1}, {name:"父节点2",id:2}, ] array_diff(arr1, arr2)
返回的数据为:
[
{name:"父节点3",id:3},
{name:"父节点4",id:4}
]