0

How can I push the values of arr1 into the arr2 ?

P.S. I know could simply accomplish this with:

arr2 = arr1 or var arr2 =[...arr1]

or by using similar solutions instead I want to push the arr1 values into the empty arr2 as seen below and without touching the arr2 at all.

This var arr2 = []; MUST be left as it is.


var arr1 = [33, 45, -10, 15, 5, 7, -5, 1, -30];

var arr2 =[];


console.log(arr2); 
// This should output: (9) [33, 45, -10, 15, 5, 7, -5, 1, -30]
// and NOT [Array(9)]



  • I've found a solution guys: `arr1.forEach(el =>arr2.push(el))` – Frank Montana Mar 04 '19 at 21:04
  • @p.s.w.g This question isn't a duplicate of the one linked, as the edit shows. Better dupe here: https://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array-without-creating – Hans Brende Mar 09 '19 at 21:21
  • @HansBrende I agree that given the updated requirements, that's a better duplicate. I've voted to reopen the question, but I'm not able to close the question again because I had previously voted to close it. For future reference, you almost never need to notify a specific user for issues like this. If you feel a question or answer needs some attention, you can post on [meta](https://meta.stackoverflow.com/) or an appropriate [char room](https://chat.stackoverflow.com/rooms/17/javascript), or even flag the question. – p.s.w.g Mar 10 '19 at 00:05
  • 1
    Possible duplicate of [How to extend an existing JavaScript array with another array, without creating a new array](https://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array-without-creating) – Hans Brende Mar 10 '19 at 00:26
  • 1
    @p.s.w.g Thanks. I had already tried flagging the question but my flag was declined with the following message: `declined - Moderator flags must not be used to resolve wrong dupe closures. https://meta.stackoverflow.com/q/291977. @ping the gold badge user who closed the question in the comments and inform them the same.` – Hans Brende Mar 10 '19 at 00:28
  • @HansBrende Fair enough. I think the last time I read a meta post about conduct surrounding duplicate questions, the recommendation was to use chat or meta--with flags mostly for abuse of some sort--but not the reply system IIRC. That of was before [Mjölnir](https://meta.stackoverflow.com/questions/tagged/dupehammer), so recommendations may be a bit different now. – p.s.w.g Mar 10 '19 at 02:01

3 Answers3

3
var arr1 = [33, 45, -10, 15, 5, 7, -5, 1, -30];
var arr2 =[...arr1];
Guy Yogev
  • 861
  • 5
  • 14
  • [IE11 support of the spread operator](http://kangax.github.io/compat-table/es6/#ie11) (Spoiler: lots of red) – isherwood Mar 04 '19 at 20:31
0

Try using the spread Operator.

Daniel Dees
  • 182
  • 2
  • 14
  • Link-only answers are not considered valuable on Stack Exchange. They die and your answer dies with them. – isherwood Mar 04 '19 at 20:32
0

Would concat work? Something like this?

var a = [1, 2, 3];
var b = [4, 5, 6];
var c = a.concat(b);
noobius
  • 1,529
  • 7
  • 14