What’s a way to append a value to an array?
This question already has an answer here:
Make a new array.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
And then push values like this
fruits.push("Kiwi");
You can do this:
// create a new array, using the `literals` instead of constructor
var array = [];
// add a value to the last position + 1 (that happens to be array.length)
array[array.length] = 10;
or
// use the push method from Array.
array.push(10);
Also, if you have an Object and you want it to behave like an array (not recommended), you can use
Array.prototype.push.call(objectLikeArray, 10);
链接地址: http://www.djcxy.com/p/17940.html
下一篇: 什么是将值附加到数组的方法?