简体   繁体   中英

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 :

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

and the result like this:

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

Avoid using 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(3) will create an array with 3 items: [undefined, undefined, undefined] .
  • new Array(3, 4) will create an array containing the items 3 and 4 : [3, 4] .

Push adds a new element to the end of the array. You create the array with three empty entries then push somethign onto the end. Javascript does not have limited sized arrays. 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. 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.

The push method looks at the length property when it adds an item, so doing this:

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. use array.unshift() method to push from starting index of array

Because in javascript "var users = new Array(3);" means creation of array with 3 undefined elements.

For basic Array initialization details you can refer to : http://www.w3schools.com/js/js_arrays.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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