-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'] 
4
  • 2
    developer.mozilla.org/en-US/docs/JavaScript/Reference/… Commented Sep 19, 2012 at 14:35
  • Thank you for the down votes, guys. :/ Commented Sep 19, 2012 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 :) Commented Sep 19, 2012 at 14:45
  • I did search but since I'm not a english speaker, I haven't remembered about the term 'merge'. No problem! :) Commented Sep 19, 2012 at 14:49

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Please don't link to W3schools - this is a much more trustworthy resource.
@eva - now I can upvote your answer. I recommend you install this userscript to prevent such things happening again :)
2

Use Array.concat:

a = a.concat(b); // a == ['a','b','c','d','e','f']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.