简体   繁体   English

当将元素推入大小数组时,数组大小动态增长是为什么?

[英]when push an element in sized array the array size is dynamically grow why?

i'm trying to push a string elements to sized array but when pushing an element the sized array start insert the elements in the last index of the array and grow but i want to insert the first element in the index [0] and second on [1] and so on till index [3] but i don't know why it start to insert elements after the last index and grow when add more element my code : 我正在尝试将字符串元素推送到大小数组,但是在推送元素时,将大小数组开始在数组的最后一个索引中插入元素并增长,但是我想在索引[0]中插入第一个元素,然后在第二个元素上插入[1]等等,直到索引[3],但我不知道为什么它开始在最后一个索引之后插入元素,并在添加更多元素时增长:

var users = new Array(3);
users.push(someData);
console.log(users);
console.log(users.length)

and the result like this: 结果是这样的:

[ , , , 'd', 'dd' ] [,,,'d','dd']
5 5

Avoid using new Array(count) . 避免使用new Array(count) Just create an empty array and then push to it like that: 只需创建一个空数组,然后像这样推入即可:

var users = [];
users.push(someData);

Or, if you have some initial static content, this will be better: 或者,如果您有一些初始静态内容,则更好:

var users = [ someData ];

The reason why I advise you to avoid new Array(...) is because it can be confusing and error-prone: 我建议您避免使用new Array(...)的原因是因为它可能造成混淆并且容易出错:

  • new Array(3) will create an array with 3 items: [undefined, undefined, undefined] . new Array(3)将创建一个包含3个项目的数组: [undefined, undefined, undefined]
  • new Array(3, 4) will create an array containing the items 3 and 4 : [3, 4] . new Array(3, 4)将创建一个包含项34的数组: [3, 4]

Push adds a new element to the end of the array. Push将新元素添加到数组的末尾。 You create the array with three empty entries then push somethign onto the end. 您用三个空条目创建数组,然后将somethign推到末尾。 Javascript does not have limited sized arrays. Javascript没有大小限制的数组。 In your case I would guess you want to create an empty arraya nd just push onto it. 在您的情况下,我想您想创建一个空数组并推入它。

The argument you pass to the new Array call sets the initial size, not the maximum size. 您传递给新Array调用的参数将设置初始大小,而不是最大大小。 There is no max size. 没有最大大小。 Just create the array like this: 像这样创建数组:

var users = new Array()

or 要么

var users = []

When you create an array with the constructor and specify a size, that will only set the current length of the array, it won't limit the size of the array. 使用构造函数创建数组并指定大小时,该大小只会设置数组的当前长度,而不会限制数组的大小。

Doing this: 这样做:

var users = new Array(3);

does exactly the same thing as this: 与此完全相同:

var users = new Array();
users.length = 3;

Growing the array by setting the length doesn't actually put anything in the array, it will only set the length property. 通过设置长度来增长数组实际上并不会在数组中放置任何内容,它只会设置length属性。

The push method looks at the length property when it adds an item, so doing this: push方法在添加项目时会查看length属性,因此请执行以下操作:

users.push(someData);

does exactly the same thing as: 与以下内容完全相同:

users[users.length] = someData;

If you want your array to be empty from the start, just create it without setting the length: 如果您希望数组从一开始就为空,则只需创建它而无需设置长度:

var users = new Array();

You can also use the array literal syntax to create an empty array: 您还可以使用数组文字语法创建一个空数组:

var users = [];

Arrays doesn't support limiting the size, so if you want a collection with a limited size, you have to create it yourself (or find one that someone else has created). 数组不支持限制大小,因此,如果要使用有限大小的集合,则必须自己创建(或找到其他人创建的集合)。

array.push() method of array insert element from last index of array. 数组插入元素的array.push()方法,从数组的最后一个索引开始。 use array.unshift() method to push from starting index of array 使用array.unshift()方法从数组的起始索引开始

Because in javascript "var users = new Array(3);" 因为在javascript中,“ var users = new Array(3);” means creation of array with 3 undefined elements. 表示创建具有3个未定义元素的数组。

For basic Array initialization details you can refer to : http://www.w3schools.com/js/js_arrays.asp 有关基本数组初始化的详细信息,请参见: http : //www.w3schools.com/js/js_arrays.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM