在 Javascript 中查找数组长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28811911/
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
Find Array length in Javascript
提问by Yameen
This will sound a very silly question. How can we find the exact length of array in JavaScript; more precisely i want to find the total positions occupied in the array.
这听起来是一个非常愚蠢的问题。我们如何在 JavaScript 中找到数组的确切长度;更准确地说,我想找到数组中占据的总位置。
I have a simple scenario which i guess most of you might be aware of.
我有一个简单的场景,我想你们大多数人可能都知道。
var a = [1,2,3];
a.length; //This will output 3
Now if i give
现在如果我给
a[100] = 100;
a.length; // The output is 101;
I want to get the exact size of the array which in the above case should be 4.
我想获得数组的确切大小,在上述情况下应该是 4。
回答by Ga?l Barbin
This will give you the number of elements really present in your array:
这将为您提供数组中真正存在的元素数量:
Object.keys(a).length
edit
编辑
As pointed in comments by JLRishe, Object.keys(a).lengthreturns the number of keys present in the object a. If I add a key like that:
正如 JLRishe 在评论中指出的那样,Object.keys(a).length返回对象 a 中存在的键数。如果我添加这样的键:
a= [1,2,3];
a['key']= "akey";
then Object.keys(a).lengthwill return 4
然后 Object.keys(a).length将返回 4
but "key" is not a part of the array, it's a property of the array Object, console.log(a)gives you [1,2,3].
但是“key”不是数组的一部分,它是数组对象的一个属性, console.log(a)给你 [1,2,3]。
So, to have a more reliable value of the number of elements in an array, we need to count only positive integer key values:
因此,为了获得更可靠的数组元素数量值,我们只需要计算正整数键值:
Object.keys(a).filter(function(el){return !(+el % 1) && +el>=0 && +el < Math.pow(2,32) ;}).length
Object.keys returns an array of String. Assigning a value in an array with a['1']
or a[1]
is equivalent.
Array index keys have also to be less than 2^32.
Object.keys 返回一个字符串数组。使用a['1']
或a[1]
为数组中的值赋值是等效的。数组索引键也必须小于 2^32。
回答by JLRishe
TL;DR The simplest reliable approach that I can think of is the following:
TL;DR 我能想到的最简单可靠的方法如下:
var count = a.filter(function() { return true; }).length;
In modern JavaScript engines, this could be shortened to:
在现代 JavaScript 引擎中,这可以缩短为:
var count = a.filter(() => true).length;
完整答案:
Checking against undefined
isn't enough because the array could actually containundefined
values.
检查undefined
是不够的,因为数组实际上可以包含undefined
值。
Reliable ways to find the number of elements are...
找到元素数量的可靠方法是......
Use the in
operator:
使用in
运算符:
var count = 0;
for (var i = 0; i < a.length; i += 1) {
if (i in a) {
count += 1;
}
}
use .forEach()
(which basically uses in
under the hood):
使用.forEach()
(基本上是in
在引擎盖下使用):
var a = [1, undefined, null, 7];
a[50] = undefined;
a[90] = 10;
var count = 0;
a.forEach(function () {
count += 1;
});
console.log(count); // 6
or use .filter()
with a predicate that is always true:
或.filter()
与始终为真的谓词一起使用:
var a = [1, undefined, null, 7];
a[50] = undefined;
a[90] = 10;
var count = a.filter(function () { return true; }).length;
console.log(count); // 6
回答by Caio César S. Leonardi
the size of the array is exactly what is shown by the length property.
数组的大小正是 length 属性显示的大小。
even though you gave a value only to the first 3 positions(0,1,2), when you gave the value 100 to the a[100], you resized it to 101 positions. Therefore, you'll get the count of every position, from 0(first index) to 100(last index), even if they have no value.
即使您只为前 3 个位置 (0,1,2) 赋值,当您将值 100 赋予 a[100] 时,您将其大小调整为 101 个位置。因此,您将获得每个位置的计数,从 0(第一个索引)到 100(最后一个索引),即使它们没有价值。
回答by jscroggs
With your code: a[100] = 100
, you are creating a block at location 100 in your array. (Total of 101 since it starts at 0).
使用您的代码:a[100] = 100
,您正在数组中的位置 100 创建一个块。(总共 101,因为它从 0 开始)。
If you want to see how many actual answers are in the block you would have to use a loop and cycle through checking which fields are not equal to undefined. There are dozens of ways to do this with for
, while
, foreach
, etc.
如果您想查看块中有多少实际答案,则必须使用循环并循环检查哪些字段不等于未定义。有几十种与要做到这一点for
,while
,foreach
,等。
Just implement a counter and count the sections in the array that are not equal to null
or undefined
. That will give you the number of places that are actually being used in the array.
只需实现一个计数器并计算数组中不等于null
or 的部分undefined
。这将为您提供数组中实际使用的位置数。