2018-08-04 javascript 獲取一個(gè)數(shù)組中重復(fù)的元素及個(gè)數(shù)
function getRepeatNum(ary){
var list = [];
var listMap = {};
for (var i = 0, len = ary.length; i < len; i++) {
key = ary[i]; // key為不重復(fù)的元素,值為number
if (!!listMap[key]) {
listMap[key]++;
} else {
listMap[key] = 1;
}
}
for (var item in listMap) {
list.push({
value: item,
number: listMap[item]
})
}
return list
}