项目中需要对比两个数组的code,删减成一个没有第二个数组code的数组
错误案例:
arr.map((item, index) => {
child.map(cl => {
if (item.code === cl.code) {
arr.splice(index, 1)
}
})
})
正确案例:
var newArr = []
arr.map((item, index) => {
if (
child.every((cl) => {
return item.code !== cl.code
})// true/false
) {
newArr.push(item)
}
})
every数组方法 return出一个判断条件,当数组全部为true,说明没有一样的就用push加上
其实也就是避免了使用splice而已