示例:以aabccdeefff为例
var arr1 = "aabccdeefff";
var has = [];
arr1 = arr1.split('');
for(var i = 0; i < arr1.length; i++) {
var num = arr1[i];
if(has.indexOf(arr1[i]) == -1) {
has.push(arr1[i]);
}
if(has[num]) { //如果存在
has[num]++;
} else { //如果没有
has[num] = 1;
}
}
console.log(has)
封装成可传参的函数
var arr1 = "aabccdeefff"; //传入参数
function getArr(arr1){
var has = [];
arr1 = arr1.split('');
for(var i = 0; i < arr1.length; i++) {
var num = arr1[i];
if(has.indexOf(arr1[i]) == -1) {
has.push(arr1[i]);
}
if(has[num]) { //如果存在
has[num]++;
} else { //如果没有
has[num] = 1;
}
}
return has;
}