compare (beforeArr, afterArr) {
let resObj = {
add: [],
del: []
}
let cenObj = {}
// 把beforeArr数组去重放入cenObj
for (let i = 0; i < beforeArr.length; i++) {
cenObj[beforeArr[i]] = beforeArr[i]
}
// 遍历afterArr,查看其元素是否在cenObj中
for (let j = 0; j < afterArr.length; j++) {
if (!cenObj[afterArr[j]]) {
resObj.add.push(afterArr[j])
} else {
delete cenObj[afterArr[j]]
}
}
for (const key in cenObj) {
resObj.del.push(key)
}
return resObj
},
网友评论