对象数组去重并且获取重复的次数

var arr = [
{name: 'aa',age: 1},
{name: 'bb',age: 1},
{name: 'cc',age: 1},
{name: 'cc',age: 1},
{name: 'cc',age: 1},
{name: 'dd',age: 1},
{name: 'dd',age: 1},
{name: 'cc',age: 1},
{name: 'cc',age: 1},
{name: 'dd',age: 1},
{name: 'dd',age: 1},
{name: 'e',age: 1},
{name: 'e',age: 1},
{name: 'dd',age: 1},
];
//添加属性number:次数
arr.forEach(tem => {
tem.number = 1;
});
//去重,并添加次数
for (var i = 0;i < arr.length; i++){
for (var j = i+1;j < arr.length;j++){
if (arr[i].name == arr[j].name){
arr.splice(j,1);
j--;
arr[i].number++;
}
}
}
posted @ 2021-06-21 18:45  凌波漫步~  阅读(243)  评论(0编辑  收藏  举报