数组对象去重根据指定属性去重,并拼接上后续的属性值
作者:互联网
const arr=[
{name:'张三',type:'person'},
{name:'李四',type:'person'},
{name:'旺财',type:'dog'},
{name:'小花',type:'cat'},
{name:'王五',type:'person'}
]
根据type进行去重,并拼接上后续的name属性值
const tempArr = []
const newArr = []
arr.map(item=>{
if(tempArr.indexOf(item.type) === -1){
tempArr.push(item.type)
newArr.push(item)
}else{
console.log(newArr[tempArr.indexOf(item.type)])
newArr[tempArr.indexOf(item.type)].name += item.name
}
})
标签:tempArr,const,name,newArr,item,拼接,数组,type,属性 来源: https://blog.csdn.net/qq_38868959/article/details/121584792