json记载字符个数

var a = ['a','a','a','b','b','c','c','c','c','a','b','c'];//声明数组
var json = {a:0,b:0,c:0};//声明json
/**
* forEach便利数组
*/
a.forEach(function(val,index){
jsonS(val);//调用函数 传入参数 val是数组的值 index是下标
});
/**
* 拼装json
* @param val 参数
*/
function jsonS(val){
for(var x in json){
if(val == x){
json[x]++;//记载个数
}
}
}
console.log(json);//打印结果
对于json封装还有问题,而且这代码有局限性
posted @ 2017-05-25 14:22  余光cc  阅读(191)  评论(3编辑  收藏  举报