计算数组中的次数[重复]

时间:2021-09-11 02:59:55

This question already has an answer here:

这个问题在这里已有答案:

I'm interested in finding out how many times every object in an array occurs.

我很想知道数组中每个对象出现的次数。

Here's what I want to do mixed with some pseudo-code of what I think is the way to do it.

这就是我想要做的事情,混合了一些我认为是这样做的伪代码。

var myArray = [1,2,3,1,1,3,4];

for (i = 0; i < myArray.length; i++) {
    if (myArray[i].AlreadyExistsInAVariable) {
        var[i]+ = 1;
    }
    else {
        CreateAnewVar();
    }
}

var one = 3;
var two = 1;
var three = 2;
var four = 1;

3 个解决方案

#1


-1  

You can't actually just create new variable bindings like that. You can, however, dynamically add new properties to an object, which is probably a better match for this problem. Here's an example that counts up the occurrences of numbers in that array:

实际上你不能像这样创建新的变量绑定。但是,您可以动态地向对象添加新属性,这可能是此问题的更好匹配。这是一个计算该数组中数字出现次数的示例:

var myArray = [1,2,3,1,1,3,4];

var counts = myArray.reduce(function(result, item) {
    result[item] || (result[item] = 0);
    result[item]++;
    return result;
}, {});

console.log(counts);

See it in action here: http://jsbin.com/huduroyite/edit?js,console

请在此处查看:http://jsbin.com/huduroyite/edit?js,console

#2


0  

You should use object to count the occurence of numbers in the array rather than using variables.

您应该使用object来计算数组中数字的出现而不是使用变量。

var myArray = [1,2,3,1,1,3,4];
var counter = {};
for (i = 0; i < myArray.length; i++) {
    if (myArray.indexOf(myArray[i])>=0) {
        if(!counter[myArray[i]]){
           counter[myArray[i]]=1;
        }
        else{
           counter[myArray[i]]++;  
        }    
    }
}
console.log(counter);

#3


0  

How about this:

这个怎么样:

var myArray = [1,2,3,1,1,3,4];

var count = myArray.reduce(function(n, val) {
    return n + (val === 1);
}, 0);

console.log(count);

Edit:

For counting occurrences of every element in an array i would do something like this:

为了计算数组中每个元素的出现次数,我会做这样的事情:

var result = {};

[1,2,3,1,1,3,4].forEach(function(e, i) {
     result[e] = result[e] + 1 || 1;
});

alert(JSON.stringify(result, null, "\t"));

#1


-1  

You can't actually just create new variable bindings like that. You can, however, dynamically add new properties to an object, which is probably a better match for this problem. Here's an example that counts up the occurrences of numbers in that array:

实际上你不能像这样创建新的变量绑定。但是,您可以动态地向对象添加新属性,这可能是此问题的更好匹配。这是一个计算该数组中数字出现次数的示例:

var myArray = [1,2,3,1,1,3,4];

var counts = myArray.reduce(function(result, item) {
    result[item] || (result[item] = 0);
    result[item]++;
    return result;
}, {});

console.log(counts);

See it in action here: http://jsbin.com/huduroyite/edit?js,console

请在此处查看:http://jsbin.com/huduroyite/edit?js,console

#2


0  

You should use object to count the occurence of numbers in the array rather than using variables.

您应该使用object来计算数组中数字的出现而不是使用变量。

var myArray = [1,2,3,1,1,3,4];
var counter = {};
for (i = 0; i < myArray.length; i++) {
    if (myArray.indexOf(myArray[i])>=0) {
        if(!counter[myArray[i]]){
           counter[myArray[i]]=1;
        }
        else{
           counter[myArray[i]]++;  
        }    
    }
}
console.log(counter);

#3


0  

How about this:

这个怎么样:

var myArray = [1,2,3,1,1,3,4];

var count = myArray.reduce(function(n, val) {
    return n + (val === 1);
}, 0);

console.log(count);

Edit:

For counting occurrences of every element in an array i would do something like this:

为了计算数组中每个元素的出现次数,我会做这样的事情:

var result = {};

[1,2,3,1,1,3,4].forEach(function(e, i) {
     result[e] = result[e] + 1 || 1;
});

alert(JSON.stringify(result, null, "\t"));