js 数组对象合并去重(简洁)
<script>
let json1=[
{id:1,name:"aaa"},
{id:2,name:"bbb",value:'111'},
{id:3,name:"ccc"},
]
let json2=[
{id:1,name:"aaa"},
{id:2,name:"bbb",value:'222'},
{id:4,name:"ddd"},
]
let json = [...json1,...json2]; //两个数组对象合并 ---就会打印bbb---111
// let json = [...json2,...json1]; //两个数组对象合并 ---就会打印bbb---222
let hash1 = {}
json = json.reduce((preVal, curVal) => {
hash1[curVal.name] ? '' : hash1[curVal.name] = true && preVal.push(curVal)
return preVal
}, [])
console.log("json",json);
// {id: 1, name: 'aaa'}
// {id:2,name:"bbb",value:'111'},
// {id:2,name:"bbb",value:'222'},
// {id: 3, name: 'ccc'}
// {id: 4, name: 'ddd'}
</script>