js 对象、数组对象覆盖
1.数组和数组对象覆盖合并
var arrObject = [ {id:1,name: 'zs'}, {id:2,name:'ls'}, {id:3,name:'we'}, ]; var object = {id:1,name:'ww'}; arrObject .forEach((val, key) => { if (val.id=== object.id) { Object.assign(val, object); } }) console.log(arrObject)// [{"id":1,"name":"ww"},{"id":2,"name":"ls"},{"id":3,"name":"we"}]
2.数组对象去重 var dataObjectArray = [ {id:1,name:'zs'}, {id:2,name: 'ww'}, {id:1,name:'ls'}, {id:3,name:'ls'}, {id:4,name:'we'}, {id:2,name:'wmz'} ]; //使用obj来记录push过的值
var obj = {};
var result = [];
for(var i =0; i < dataObjectArray.length; i++){ console.log(obj)//{},{"1":true},{"1":true,"2":true},{"1":true,"2":true},{"1":true,"2":true,"3":true},{"1":true,"2":true,"3":true,"4":true} if(!obj[dataObjectArray[i].name]){ result.push(dataObjectArray[i]); obj[dataObjectArray[i].name] = true; } } console.log(result)// [{"id":1,"name":"zs"},{"id":2,"name":"ww"},{"id":3,"name":"ls"},{"id":4,"name":"we"}]