第一种:
var a=['a','a','a','b','b','c','c','c','c','a','b','c'];
var len= a.length;
console.log(len);
var a_err=0;
var b_err=0;
var c_err=0;
var ass=0;
var i=0;
for(var ass=0;ass<=12 ;ass++){
if(a[i] =='a' ){
a_err++;
}else if(a[i]=='b' ){
b_err++;
}else if(a[i]=='c'){
c_err++;
}
i++;
} ;
var json={a:a_err ,b:b_err,c:c_err};
console.log(json);
第二种:
var a = ['a','a','a','b','b','c','c','c','c','d','hha','niha'];
var s = '';
var json = {};
a.forEach(function(val,index){
if(s!=val){
s = val;
}
json[s]?json[s]++:(json[s]=1);
})
console.log(json)