如何获取数组成员和它们重复的次数(重复)?
我目前有这个脚本
//COUNT VAL
var curr = '';
var previous = '';
var arr = new Array();
var sorted = count.sort();
for(var c=0; c < sorted.length; c++){
if(sorted[c] != ''){
if(sorted[c] != curr){
var repeat = 1;
arr[sorted[c]] = repeat;
curr = sorted[c];
}
else if(sorted[c] == curr){
repeat++;
}
}
}
alert(JSON.stringify(arr));
数组“count”的值是(我使用 JSON.stringify):
[" 2"," 2"," 2","1","1","1","1","1","1","1","1","1","1",null,null,null,null,null,null,null,null,null,null,null,null,null]
我希望我的脚本显示什么...(我希望它返回一个数组)
[1: 10, 2: 3]
(x: y) x 是数字,y 是它重复的次数。
我得到什么...
[null,1,1]