Is it possible to use Array.push to add multiple values from a list? For example, currently what I'm having to do is:
Array.prototype.extend = function(arr) {
for (let elem of arr) {
this.push(elem);
}
}
let x = [1,2,3];
let y = [4,5,6];
let z = [7,8,9];
x.push(y);
x.extend(z)
console.log(x);
Or, is there some way to pass the array as a single argument to the push method, or perhaps there's another Array method entirely that does this instead?
x.push.apply(x, y);x.pushreturns a[object Function].Function.prototype.applyreceivesthisand an argument list..apply()method on the Function prototype takes two arguments: the value to be used forthis, and an array. The array is treated as the function argument list. In modern JavaScript, it's the same asx.push(... y), which I'd use if I didn't have to worry about Internet Explorer.x.push(...y)?