通過一個數組找到每個元素對應出現的次數
阿新 • • 發佈:2018-11-28
var arrayObj = ['a','b','b','a','d','g'];
function countData(arrayObj){
var arrayNew = new Array();var count =0;
var temp ='';
var json ='';
var jsonArr = [];
for(var i=0;i<arrayObj.length;i++){
if(arrayObj[i]!=-1){
temp=arrayObj[i];
for(var j=0;j<arrayObj.length;j++){
if(temp == arrayObj[j]){
count++;
arrayObj[j] = -1;
}
}
json += temp+","+count+",";
count = 0;
}
}
json = json.substring(0,json.length-1);
var jsonArr = json.split(",");
arrayNew.push(json);
return jsonArr;
}