计算相同值出现在 javascript 数组中的次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37365512/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:14:36  来源:igfitidea点击:

Count the number of times a same value appears in a javascript array

javascriptarrays

提问by Donald Duck

I would like to know if there is a native javascript code that does the same thing as this:

我想知道是否有与此相同的本机 javascript 代码:

function f(array,value){
    var n = 0;
    for(i = 0; i < array.length; i++){
        if(array[i] == value){n++}
    }
    return n;
}

回答by RomanPerekhrest

There might be different approaches for such purpose.
And your approach with forloop is obviously not misplaced(except that it looks redundantly by amount of code).
Here is some additional approaches to get the occurrence of a certain value in array:

为此目的可能有不同的方法。
而且你的for循环方法显然没有放错地方(除了它的代码量看起来多余)。
这里有一些额外的方法来获取数组中某个值的出现:

  • Using Array.forEachmethod:

    var arr = [2, 3, 1, 3, 4, 5, 3, 1];
    
    function getOccurrence(array, value) {
        var count = 0;
        array.forEach((v) => (v === value && count++));
        return count;
    }
    
    console.log(getOccurrence(arr, 1));  // 2
    console.log(getOccurrence(arr, 3));  // 3
    
  • Using Array.filtermethod:

    function getOccurrence(array, value) {
        return array.filter((v) => (v === value)).length;
    }
    
    console.log(getOccurrence(arr, 1));  // 2
    console.log(getOccurrence(arr, 3));  // 3
    
  • 使用Array.forEach方法:

    var arr = [2, 3, 1, 3, 4, 5, 3, 1];
    
    function getOccurrence(array, value) {
        var count = 0;
        array.forEach((v) => (v === value && count++));
        return count;
    }
    
    console.log(getOccurrence(arr, 1));  // 2
    console.log(getOccurrence(arr, 3));  // 3
    
  • 使用Array.filter方法:

    function getOccurrence(array, value) {
        return array.filter((v) => (v === value)).length;
    }
    
    console.log(getOccurrence(arr, 1));  // 2
    console.log(getOccurrence(arr, 3));  // 3
    

回答by omarjmh

You could use reduce to get there:

您可以使用 reduce 到达那里:

Working example

工作示例

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

var map = a.reduce(function(obj, b) {
  obj[b] = ++obj[b] || 1;
  return obj;
}, {});

回答by Edgy

Another option is:

另一种选择是:

count = myArray.filter(x => x == searchValue).length;

回答by Jung In Cha

You can also use forEach

您也可以使用 forEach

let countObj = {};
let arr = [1,2,3,1,2,3,4];

let countFunc = keys => {
  countObj[keys] = ++countObj[keys] || 1;
}

arr.forEach(countFunc);

// {1: 2, 2: 2, 3: 2, 4: 1}

回答by Saeed D.

You may want to use indexOf()function to find and count each valuein array

您可能需要使用indexOf()的功能来查找和统计每个valuearray

function g(array,value){
  var n = -1;
  var i = -1;
  do {
    n++;
    i = array.indexOf(value, i+1);
  } while (i >= 0  );

  return n;
}