var str ="abcabcaaaaa";
var map = {};
for(var i = 0;i<str.length;i++){
var s = str[i];
var r = map[s];
if(r){
map[s] +=1;
}else{
map[s] = 1;
}
}
console.log(JSON.stringify(map));
输出结果:
{"a":7,"b":2,"c":2}
var str ="abcabcaaaaa";
var map = {};
for(var i = 0;i<str.length;i++){
var s = str[i];
var r = map[s];
if(r){
map[s] +=1;
}else{
map[s] = 1;
}
}
console.log(JSON.stringify(map));
输出结果:
{"a":7,"b":2,"c":2}