2018-08-04 javascript 获取一个数组中重复的元素及个数
function getRepeatNum(ary){
var list = [];
var listMap = {};
for (var i = 0, len = ary.length; i < len; i++) {
key = ary[i]; // key为不重复的元素,值为number
if (!!listMap[key]) {
listMap[key]++;
} else {
listMap[key] = 1;
}
}
for (var item in listMap) {
list.push({
value: item,
number: listMap[item]
})
}
return list
}