javascript 数组值计数javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8607455/
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
Array value count javascript
提问by user1033600
How can I count the array depending on the value.. I have an array with this value..
如何根据值计算数组.. 我有一个具有此值的数组..
var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";
I need to count the array depending on the value
我需要根据值计算数组
Array with Value A is 2 Array with value B is 1
A 值为 2 的数组 B 值为 1 的数组
Thanks!
谢谢!
回答by jessegavin
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);
If you're interested in a built-in function... you could always add your own.
如果您对内置函数感兴趣……您可以随时添加自己的函数。
Array.prototype.count = function(value) {
var counter = 0;
for(var i=0;i<this.length;i++) {
if (this[i] === value) counter++;
}
return counter;
};
Then you could use it like this.
那么你可以像这样使用它。
var result = myArr.count("a");
alert(result);
回答by MetalFrog
Oh well, beaten to the punch, but here's my version.
哦,好吧,一拳,但这是我的版本。
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;
}
回答by Muhammad Usman
Live Demo:http://jsfiddle.net/CLjyj/1/
现场演示:http : //jsfiddle.net/CLjyj/1/
var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";
function getNum(aVal)
{
num=0;
for(i=0;i<myArr.length;i++)
{
if(myArr[i]==aVal)
num++;
}
return num;
}
alert(getNum('a')); //here is the use
回答by sgimeno
I would use Array.filter
我会使用Array.filter
var arr = ['a', 'b', 'b']
arr.filter(val => val === 'b').length // 2
回答by Jee Mok
You can use Lodash's countBy
function:
您可以使用 Lodash 的countBy
功能:
_.countBy(myArr);
Example:
例子:
var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";
const result = _.countBy(myArr);
// Get the count of `a` value
console.log('a', result.a);
// Get the count of `b` value
console.log('b', result.b);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
回答by Some Guy
Everyone's already given the obvious function, so I'm going to try and make another solution:
每个人都已经给出了明显的功能,所以我将尝试做出另一个解决方案:
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'));