push()
method adds one or more elements to the end of an array and returns the new length of the array.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
arr.push([element1[, ...[, elementN]]])
element
N
The element(s) to add to the end of the array.
The new
length
property of the object upon which the method was called.
push
method appends values to an array.
push
is intentionally generic. This method can be used with
call()
or
apply()
on objects resembling arrays. The
push
method relies on a
length
property to determine where to start inserting the given values. If the
length
property cannot be converted into a number, the index used is 0. This includes the possibility of
length
being nonexistent, in which case
length
will also be created.
Although strings are native, Array-like objects, they are not suitable in applications of this method, as strings are immutable. Similarly for the native, Array-like object arguments .
The following code creates the
sports
array containing two elements, then appends two elements to it. The
total
variable contains the new length of the array.
let sports = ['soccer', 'baseball']
let total = sports.push('football', 'swimming')
console.log(sports) // ['soccer', 'baseball', 'football', 'swimming']
console.log(total) // 4
此范例使用
apply()
to push all elements from a second array.
Do
not
use this method if the second array (
moreVegs
in the example) is very large because the maximum number of parameters that one function can take is limited in practice. See
apply()
了解更多细节。
let vegetables = ['parsnip', 'potato']
let moreVegs = ['celery', 'beetroot']
// Merge the second array into the first one
// Equivalent to vegetables.push('celery', 'beetroot')
Array.prototype.push.apply(vegetables, moreVegs)
console.log(vegetables) // ['parsnip', 'potato', 'celery', 'beetroot']
As mentioned above,
push
is intentionally generic, and we can use that to our advantage.
Array.prototype.push
can work on an object just fine, as this example shows.
Note that we don't create an array to store a collection of objects. Instead, we store the collection on the object itself and use
call
on
Array.prototype.push
to trick the method into thinking we are dealing with an array—and it just works, thanks to the way JavaScript allows us to establish the execution context in any way we want.
let obj = {
length: 0,
addElem: function addElem(elem) {
// obj.length is automatically incremented
// every time an element is added.
[].push.call(this, elem)
}
}
// Let's add some empty objects just to illustrate.
obj.addElem({})
obj.addElem({})
console.log(obj.length)
// → 2
Note that although
obj
is not an array, the method
push
successfully incremented
obj
's
length
property just like if we were dealing with an actual array.
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Array.prototype.push' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
push
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
Array
Array.from()
Array.isArray()
Array.of()
Array.prototype.concat()
Array.prototype.copyWithin()
Array.prototype.entries()
Array.prototype.every()
Array.prototype.fill()
Array.prototype.filter()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.flat()
Array.prototype.flatMap()
Array.prototype.forEach()
Array.prototype.includes()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.keys()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toSource()
Array.prototype.toString()
Array.prototype.unshift()
Array.prototype.values()
Array.prototype[@@iterator]()
get Array[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()