获取JS数组中的相同元素的名称和数量

<script>
var ary =["aaa","露露","bbb","aaa","ccc","ccc","aaa","露露","haha"];
var res = [];
ary.sort();
for(var i = 0;i<ary.length;)
{
 
 var count = 0;
 for(var j=i;j<ary.length;j++)
 {
     
  if(ary[i] == ary[j])
  {
   count++;
  }
  
 }
 res.push([ary[i],count]);
 i+=count;
 
}
//res 二维数维中保存了 值和值的重复数
for(var  i = 0 ;i<res.length;i++)
{
 document.writeln("值:"+res[i][0]+"           重复次数:"+res[i][1]+"<br/>");
}

</script>

 

你可能感兴趣的:(J#)