js数组对象的相同值相加合并
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
<script>
// js数组对象相同值相加,不同添加
var arr = [{id:1,name:'aaa'},{id:1,name:'bbb'},{id:1,name:'bbb'},{id:2,name:"ccc"},{id:3,name:'bbb2'},{id:3,name:'bbb'},,{id:2,name:"c2cc"},,{id:555,name:"ccc"}]    function mergeArr(arr){
var newArr=[];
arr.forEach(item=>{
var dataItem =item
if(newArr.length>0){
var filterValue = newArr.filter(v=>{
return v.id == dataItem.id
})
if(filterValue.length>0){
newArr.forEach(n=>{
if( n.id ==filterValue[0].id){
n.name =  filterValue[0].name +dataItem.name
}
})
}else{
newArr.push(dataItem)
}
}else{
newArr.push(dataItem)
js合并两个数组}
})
return newArr
}
console.log(mergeArr(arr))
</script>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。