var a = [ {id: 2, nickname: "韩信", checked: false}, {id: 7, nickname: "刘邦", checked: true}, ]; var b = [ {id: 2, nickname: "韩信", checked: false}, {id: 7, nickname: "刘邦", checked: false}, {id: 8, nickname: "项羽", checked: false}, ]; var ids = {}; // 用于跟踪已经存在的id var c = a.concat(b).filter(item => { if (!ids[item.id]) { // 如果ids中不存在当前id,则将当前id添加到ids中,并保留该项 ids[item.id] = true; return true; } return false; // 如果ids中已经存在当前id,则过滤掉该项 }); console.log(c);
这里,C里的id是唯一,且A里的checked=true时,C对应的也会是true
下面是输出结果
标签:checked,true,合并,ids,JS,数组,false,nickname,id From: https://www.cnblogs.com/basstk/p/17772550.html