<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
var a = ["aaa", "aaa", "aaa", "aaa", 'a', 'aaaaaa', '哈哈哈', 'aaaaaa', 'bbbb'];
var b = {};
var c = {};
for(var i = 0; i < a.length; i++) {
if(a[i] in b) {
b[a[i]]++;
} else {
b[a[i]] = 1;
}
}
console.log(b); for(var j = 0; j < a.length; j++) {
if(c[a[j]]) {
c[a[j]]++;
} else {
c[a[j]] = 1;
}
}
console.log(c);
</script>
</head> <body>
</body> </html>

 JS返回数组种类和个数(面试常问)-LMLPHP 

05-11 17:31
查看更多