Javascript Array.size() 与 Array.length

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

Array.size() vs Array.length

javascriptarrays

提问by Abraham P

What is the difference between the two?

两者有什么区别?

So I know that array.size()is a function while array.lengthis a property. Is there a usecase for using one over the other? Is one more efficient? (I would imagine .lengthto be significantly faster as it is a property rather then a method call?) Why would one ever use the slower option? Are there some browsers that are incompatible with one or the other?

所以我知道这array.size()是一个函数array.length而是一个属性。是否有使用一个而不是另一个的用例?一个更有效吗?(我.length认为它会快得多,因为它是一个属性而不是方法调用?)为什么要使用较慢的选项?是否有一些浏览器与其中一种不兼容?

  var x = [];
  console.log(x.size());
  console.log(x.length);
  console.log(x.size()==x.length);
  x =[1,2,3];
  console.log(x.size());
  console.log(x.length);
  console.log(x.size()==x.length);

Will print:

将打印:

  0, 0, true
  3, 3, true

回答by Gabriel

Array.size() is not a valid method

Array.size() 不是有效的方法

Always use the length property

始终使用长度属性

There is a library or script adding the size method to the array prototype since this is not a native array method. This is commonly done to add support for a custom getter. An example of using this would be when you want to get the size in memory of an array (which is the only thing I can think of that would be useful for this name).

有一个库或脚本将 size 方法添加到数组原型中,因为这不是本机数组方法。通常这样做是为了添加对自定义 getter 的支持。使用它的一个示例是当您想要获取数组内存中的大小时(这是我能想到的唯一对这个名称有用的东西)。

Underscore.js unfortunately defines a sizemethod which actually returns the length of an object or array. Since unfortunately the length property of a function is defined as the number of named arguments the function declares they had to use an alternative and size was chosen (count would have been a better choice).

不幸的是size,Underscore.js 定义了一个实际返回对象或数组长度的方法。不幸的是,函数的长度属性被定义为函数声明它们必须使用替代方法的命名参数的数量,并选择了大小(计数将是更好的选择)。

回答by Naftali aka Neal

.size()is nota native JS function of Array(at least not in any browser that I know of).

.size()不是土生土长JS功能Array(至少不会在任何浏览器,我知道的)。

.lengthshouldbe used.

.length应该使用。



If

如果

.size()doeswork on your page, make sure you do not have any extra libraries included like prototypethat is mucking with the Arrayprototype.

.size()确实在您的页面工作,请确保您没有包含任何额外的库,例如与原型混淆的Array原型。

or

或者

There might be some plugin on your browser that is mucking with the Arrayprototype.

您的浏览器上可能有一些插件与Array原型混淆。

回答by Saurabh Chandra Patel

The .size()function is available in Jquery and many other libraries.

.size()函数在 Jquery 和许多其他库中可用。

The .lengthproperty works only when the index is an integer.

.length属性仅在索引为整数时有效。

The lengthproperty willwork with this type of array:

length属性与这种类型的数组一起使用:

var nums = new Array();
nums[0] = 1; 
nums[1] = 2;
print(nums.length); // displays 2

The lengthproperty won'twork with this type of array:

length属性不适用于这种类型的数组:

var pbook = new Array(); 
pbook["David"] = 1; 
pbook["Jennifer"] = 2;
print(pbook.length); // displays 0

So in your case you should be using the .lengthproperty.

因此,在您的情况下,您应该使用该.length属性。

回答by Julio Flores

.size()is jQuery's, much probably you're either confusing with or took from someone else who had imported the jQuery library to his project.

.size()是 jQuery 的,很可能你会混淆或从其他人那里获取 jQuery 库到他的项目。

If you'd have jQueryimported and you'd write like $(array).size(), it would return the array length.

如果你已经导入了jQuery并且你会这样写$(array).size(),它会返回数组长度。

回答by user2811108

array.lengthisn't necessarily the number of items in the array:

array.length不一定是数组中的项目数:

var a = ['car1', 'car2', 'car3'];
a[100] = 'car100';
a.length; // 101

The length of the array is one more than the highest index.

数组的长度比最高索引多一。

As stated before Array.size()is not a valid method.

如前所述,Array.size()这不是一种有效的方法。

More information

更多信息

回答by cskwg

The property 'length' returns the (last_key + 1) for arrays with numeric keys:

对于带有数字键的数组,属性 'length' 返回 (last_key + 1):

var  nums  =  new  Array ();
nums [ 10 ]  =  10 ; 
nums [ 11 ]  =  11 ;
log.info( nums.length );

will print 12!

将打印 12!

This will work:

这将起作用:

var  nums  =  new  Array ();
nums [ 10 ]  =  10 ; 
nums [ 11 ]  =  11 ;
nums [ 12 ]  =  12 ;
log.info( nums.length + '  /  '+ Object.keys(nums).length );

回答by Davy

The .size() method is deprecated as of jQuery 1.8. Use the .length property instead

.size() 方法从 jQuery 1.8 开始被弃用。改用 .length 属性

See: https://api.jquery.com/size/

见:https: //api.jquery.com/size/

回答by rev

Size detects duplicates, it will return the number of unique values

大小检测重复,它将返回唯一值的数量

const set1 = new Set([1, 2, 3, 4, 5, 5, 5, 6]);
console.log(set1.size);
// expected output: 6

回答by AmerllicA

Actually, .size()is not pure JavaScript method, there is a accessor property .sizeof Setobject that is a little looks like .size()but it is not a function method, just as I said, it is an accessor property of a Set object to show the unique number of (unique) elements in a Set object.

其实.size()不是纯JavaScript方法,Set对象有一个accessor属性.size,有点像但不是函数方法,正如我说的,它是一个Set对象的accessor属性,用来显示唯一的编号Set 对象中的(唯一)元素。.size()

The size accessor property returns the number of (unique) elements in a Set object.

size 访问器属性返回 Set 对象中(唯一)元素的数量。

const set1 = new Set();
const object1 = new Object();

set1.add(42);
set1.add('forty two');
set1.add('forty two');
set1.add(object1);

console.log(set1.size);
// expected output: 3

And lengthis a property of an iterable object(array) that returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

Andlength是一个可迭代对象(数组)的属性,它返回该数组中的元素数。该值是一个无符号的 32 位整数,在数字上始终大于数组中的最高索引。

const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log(clothing.length);
// expected output: 4

回答by Sunali Bandara

we can you use .lengthproperty to set or returns number of elements in an array. return value is a number

我们可以使用.length属性来设置或返回数组中的元素数。返回值是一个数字

> set the length: let count = myArray.length;
> return lengthof an array : myArray.length

we can you .size in case we need to filter duplicate values and get the count of elements in a set.

如果我们需要过滤重复值并获取集合中元素的数量,我们可以使用 .size。

const set = new set([1,1,2,1]); 
 console.log(set.size) ;`
const set = new set([1,1,2,1]); 
 console.log(set.size) ;`