What’s a way to append a value to an array?
本问题已经有最佳答案,请猛点这里访问。
附加到数组
如何在javascript中附加到数组
创建新数组。
| 1 | var fruits = ["Banana","Orange","Apple","Mango"]; | 
然后像这样推值
| 1 | fruits.push("Kiwi"); | 
您可以这样做:
| 1 2 3 4 5 | // 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; | 
或
| 1 2 |  // use the push method from Array. array.push(10); | 
此外,如果您有一个对象,并且希望它的行为类似于数组(不推荐),则可以使用
| 1 | Array.prototype.push.call(objectLikeArray, 10); |