陣列中每個陣列元素出現的次數

浪裡白條那片海發表於2018-06-05
<script>
var obj = [1,4,2,1,3,2,1,4];
var o = {};


for (var index in obj){
if(o.hasOwnProperty(obj[index])){
var obj1 = obj[index];
o[obj1] = o[obj1]+1;
}else{
var obj1 = obj[index];
o[obj1] = 1;
}
}
// console.log(o);
for(k in o){
console.log(k+"出現了"+o[k]+"次")
}
</script>

相關文章