JS判断数组的值出现的次数,以及去重

时间:2021-10-29 20:34:14
        var arr = ["曹阳","曹阳","曹阳","张三","张三","张三","张三","张三",];
var temp = [];
var obj = {};
var newArr = [];
for (var i = ; i < arr.length; i++) {
var dt = arr[i];
if (obj[dt]) {
obj[dt]++;
} else {
temp.push(dt);
obj[dt] = ;
}
}
console.log(temp);
for (const i in obj) {
newArr.push({
'name':i,
'value':obj[i]
})
}
console.log(newArr);