-1

Possible Duplicate:
How to merge two arrays in Javascript

Let's suppose I have 2 arrays:

a = ['a','b','c'];
b = ['d','e','f'];

Is there somehow to easy add the b into a without having to split and perform an each for adding the elements?

Something like:

a.push(b);

And have an final a array with the content:

a = ['a','b','c','d','e','f'] 
Community
  • 1
  • 1
Kleber S.
  • 7,717
  • 6
  • 39
  • 66
  • 2
    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/concat – chrisfrancis27 Sep 19 '12 at 14:35
  • Thank you for the down votes, guys. :/ – Kleber S. Sep 19 '12 at 14:42
  • @kle - One of the reasons for downvoting is a lack of research. There is nothing wrong with your actual post. The problem is that with one simple search you wouldn't have had to ask this in the first place. It's nothing personal :) – Lix Sep 19 '12 at 14:45
  • I did search but since I'm not a english speaker, I haven't remembered about the term 'merge'. No problem! :) – Kleber S. Sep 19 '12 at 14:49

2 Answers2

6

Have you tryied the concat() function?

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var kai = ["Robin"];
var children = hege.concat(stale,kai);

Will output:

Cecilie,Lone,Emil,Tobias,Linus,Robin
zzzzBov
  • 157,699
  • 47
  • 307
  • 349
Evandro Silva
  • 1,352
  • 14
  • 29
  • 4
    Please don't link to W3schools - [this is a much more trustworthy resource](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/concat). – chrisfrancis27 Sep 19 '12 at 14:36
  • 1
    @eva - **now** I can upvote your answer. I recommend you install [this userscript](http://userscripts.org/scripts/review/8367) to prevent such things happening again :) – Lix Sep 19 '12 at 14:38
2

Use Array.concat:

a = a.concat(b); // a == ['a','b','c','d','e','f']
rgthree
  • 6,849
  • 15
  • 20