Javascript 如何计算数组中每个项目的出现次数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11649255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count the number of occurrences of each item in an array?
提问by Aadi
I have an array as follows,
我有一个数组如下,
var arr = ['ab','pq','mn','ab','mn','ab']
Expected result
预期结果
arr['ab'] = 3
arr['pq'] = 1
arr['mn'] = 2
Tried as follows,
尝试如下,
$.each(arr, function (index, value) {
if (value)
arr[value] = (resultSummary[value]) ? arr[value] + 1 : 1;
});
console.log(arr.join(','));
回答by Fabrizio Calderan
no need to use jQuery for this task — this example will build an object with the amount of occurencies of every different element in the array in O(n)
不需要使用 jQuery 来完成这个任务——这个例子将构建一个对象,其中包含数组中每个不同元素的出现次数 O(n)
var occurrences = { };
for (var i = 0, j = arr.length; i < j; i++) {
occurrences[arr[i]] = (occurrences[arr[i]] || 0) + 1;
}
console.log(occurrences); // {ab: 3, pq: 1, mn: 2}
console.log(occurrences['mn']); // 2
You could also use Array.reduceto obtain the same result and avoid a for-loop
您也可以使用Array.reduce来获得相同的结果并避免for-loop
var occurrences = arr.reduce(function(obj, item) {
obj[item] = (obj[item] || 0) + 1;
return obj;
}, {});
console.log(occurrences); // {ab: 3, pq: 1, mn: 2}
console.log(occurrences['mn']); // 2
回答by Dmytro Kozlovskyi
I think this is the simplest way how to count occurrences with same value in array.
我认为这是如何计算数组中具有相同值的出现次数的最简单方法。
var a = [true, false, false, false];
a.filter(function(value){
return value === false;
}).length
回答by code_monk
if you don't want a solution that requires a library, and don't have to support legacy javascript:
如果您不想要需要库的解决方案,并且不必支持旧版 javascript:
var report = {};
arr.forEach(function(el){
report[el] = report[el] + 1 || 1;
});
Or if you want to do it using jQuery:
或者,如果您想使用 jQuery 执行此操作:
var report = {};
$.each(arr,function(i,el){
report[el] = report[el] + 1 || 1;
});
This uses short-circuit logicto test for conditions and set values. I think it's a pretty concise and readable way to do javascript.
这使用短路逻辑来测试条件和设定值。我认为这是一种非常简洁易读的 javascript 方法。
console.log( report );
回答by rsp
回答by meadlai
var result = {};
function count(input){
var tmp = 0;
if(result.hasOwnProperty(input)){
tmp = result[input];
result[input]=tmp+1;
}else{
result[input]=1;
}
}
above function will help you to count the num of the same string in an Array.
上面的函数将帮助您计算数组中相同字符串的数量。
回答by Hymanwin tung
回答by codebox
Could this be what you are trying to do?
这可能是你想要做的吗?
$.each(arr, function(index, value) {
if (!resultSummary[value]){
resultSummary[value] = 0;
}
resultSummary[value] += 1;
});
this code will count the occurrences of each string in the array and store the results in resultsArray
此代码将计算数组中每个字符串的出现次数并将结果存储在 resultsArray
回答by spaceman12
var arr = ['ab','pq','mn','ab','mn','ab']
function getCount(arr,val)
{
var ob={};
var len=arr.length;
for(var k=0;k<len;k++)
{
if(ob.hasOwnProperty(arr[k]))
{
ob[arr[k]]++;
continue;
}
ob[arr[k]]=1;
}
return ob[val];
}
//run test
alert(getCount(arr,'ab'));//3
var arr = ['ab','pq','mn','ab','mn','ab']
function getCount(arr,val)
{
var ob={};
var len=arr.length;
for(var k=0;k<len;k++)
{
if(ob.hasOwnProperty(arr[k]))
{
ob[arr[k]]++;
continue;
}
ob[arr[k]]=1;
}
return ob[val];
}
//run test
alert(getCount(arr,'ab'));//3
回答by Prashant_M
var arr = ['ab','pq','mn','ab','mn','ab'];
var result = { };
for(i=0;i<arr.length;++i)
{
if(!result[arr[i]])
result[arr[i]]=0;
++result[arr[i]];
}
for (var i in result){
console.log(i+":"+result[i]);
}