数组值计数javascript

数组值计数javascript,javascript,jquery,Javascript,Jquery,如何根据值对数组进行计数。。 我有一个具有此值的数组 var myArr = new Array(3); myArr[0] = "a"; myArr[1] = "a"; myArr[2] = "b"; 我需要根据值对数组进行计数 值为A的数组为2 值为B的数组为1 谢谢 现场演示: var myArr = new Array(3); myArr[0] = "a"; myArr[1] = "a"; myArr[2] = "b"; function count(array, value) {

如何根据值对数组进行计数。。 我有一个具有此值的数组

var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";
我需要根据值对数组进行计数

值为A的数组为2 值为B的数组为1


谢谢

现场演示:

var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";


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

var result = count(myArr, "a");
alert(result);
var myArr=新数组(3);
myArr[0]=“a”;
myArr[1]=“a”;
myArr[2]=“b”;
函数getNum(aVal)
{
num=0;

因为(i=0;i哦,好吧,被打败了,但这是我的版本

var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b"

getCountFromVal( 'a', myArr );

function getCountFromVal( val, arr )
{
    var total =  arr.length;
    var matches = 0;

    for( var i = 0; i < total; i++ )
    {
        if( arr[i] === val )
        {
            matches++;
        }
    }

    console.log(matches);
    return matches;
}
var myArr=新数组(3);
myArr[0]=“a”;
myArr[1]=“a”;
myArr[2]=“b”
getCountFromVal('a',myArr);
函数getCountFromVal(val,arr)
{
var total=平均长度;
var匹配=0;
对于(变量i=0;i
每个人都已经给出了明显的功能,因此我将尝试提出另一个解决方案:

var myArr = [];
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";

function count(arr, value){
    var tempArr = arr.join('').split(''), //Creates a copy instead of a reference
        matches = 0,
        foundIndex = tempArr.indexOf(value); //Find the index of the value's first occurence
    while(foundIndex !== -1){
        tempArr.splice(foundIndex, 1); //Remove that value so you can find the next
        foundIndex = tempArr.indexOf(value);
        matches++;
    }
    return matches;
}

//Call it this way
document.write(count(myArr, 'b'));
我会使用Array.filter


您可以使用Lodash的
countBy
功能:

_.countBy(myArr);
例如:
var myArr=新数组(3);
myArr[0]=“a”;
myArr[1]=“a”;
myArr[2]=“b”;
常量结果=uu.countBy(myArr);
//获取'a'值的计数
console.log('a',result.a);
//获取'b'值的计数
console.log('b',result.b);

谢谢,我只是觉得它有一个内置的功能:D@user1033600添加了函数的原型版本。
var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b"

getCountFromVal( 'a', myArr );

function getCountFromVal( val, arr )
{
    var total =  arr.length;
    var matches = 0;

    for( var i = 0; i < total; i++ )
    {
        if( arr[i] === val )
        {
            matches++;
        }
    }

    console.log(matches);
    return matches;
}
var myArr = [];
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";

function count(arr, value){
    var tempArr = arr.join('').split(''), //Creates a copy instead of a reference
        matches = 0,
        foundIndex = tempArr.indexOf(value); //Find the index of the value's first occurence
    while(foundIndex !== -1){
        tempArr.splice(foundIndex, 1); //Remove that value so you can find the next
        foundIndex = tempArr.indexOf(value);
        matches++;
    }
    return matches;
}

//Call it this way
document.write(count(myArr, 'b'));
  var arr = ['a', 'b', 'b']
  arr.filter(val => val === 'b').length // 2
_.countBy(myArr);