Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Array Extend without custom method?

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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It's usually bad practice to Extend Native Types with Custom Methods. So instead of adding custom ones to the Array prototype you could:

  • Push values of another Array using the Spread Syntax ...

const x = [1,2,3];
const y = [4,5,6];
const z = [7,8,9];
x.push(y);
x.push(...z)
console.log(x);

  • Concatenate, using the Array.prototype.concat()

let x = [1,2,3];
const y = [4,5,6];
const z = [7,8,9];
x.push(y);
x = x.concat(z);
console.log(x);

The difference being - .concat() does not directly modify the original Array, therefore the x = x.concat( assignment syntax, and the Original x array being set as a let variable.

about 4 years ago · Juan Pablo Isaza Report

0

You can push multiple arguments to Array.prototype.push and they'll all be pushed to our result array, so we can "convert" our array into multiple arguments using the ES6 spread syntax.

let x = [1,2,3];
let y = [4,5,6];
x.push(...y);
console.log(x);

about 4 years ago · Juan Pablo Isaza Report

0

you can spread the array element like this

const x = [1,2,3];
const y = [4,5,6];
const z = [7,8,9];
x.push(...y);
x.push(...z)
console.log(x);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!