统计数组中重复元素的个数
2019-03-04 本文已影响4人
M_Events
var arr = [1, 2, 4, 6, 6, 4, 4, 7, 1, 2, 2];
var obj = {};
for (var i = 0, len = arr.length; i <= len - 1; i++){
var item = arr[i];
obj[item] = (obj[item] + 1) || 1;
}
console.log(obj)
var arr = [1, 2, 4, 6, 6, 4, 4, 7, 1, 2, 2];
var obj = {};
for (var i = 0, len = arr.length; i <= len - 1; i++){
var item = arr[i];
obj[item] = (obj[item] + 1) || 1;
}
console.log(obj)