0

Say for example I have an array

arr = [ 1, 2, 3 4, 5, 6 ] 

Instead, I would like to use aliases for each line Ex.

var a = [ 1, 2, 3 ]; var b = [ 4, 5, 6 ]; 

Where

arr = [ a, b ] 

should be the same as the original arr.

Yet the current example gives back [ [1,2,3],[4,5,6] ] instead

How would I achieve something like this in javascript?

5
  • Javascript is not a whitespace sensitive language, line breaks will not matter. Commented Feb 1, 2015 at 4:43
  • Didn't arr = [ a, b ] work? Commented Feb 1, 2015 at 4:43
  • @thefourtheye arr = [a, b] results in [ [1,2,3],[4,5,6]] Commented Feb 1, 2015 at 4:45
  • @dk123 What exactly do you want? I couldn't understand that from the question. Can you please explain clearly with an example? Commented Feb 1, 2015 at 4:46
  • Please clarify "it isn't at the moment since I would be inserting arrays instead of actual numbers of lists" Commented Feb 1, 2015 at 4:48

2 Answers 2

6

Use Array::concat():

var a = [ 1, 2, 3 ]; var b = [ 4, 5, 6 ]; var c = [ 7, 8, 9 ]; var arr = Array.concat(a,b,c); alert(arr); // 1,2,3,4,5,6,7,8,9 

http://jsfiddle.net/9ys2ow0y/1/

Sign up to request clarification or add additional context in comments.

Comments

1

"should be the same as the original arr." NO!!!

The concat() method is used to join two or more arrays.

This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

var a = [ 1, 2, 3 ]; var b = [ 4, 5, 6 ]; arr = a.concat(b); //or Array.concat(a,b); console.log(a);//[1,2,3] console.log(b);//[4,5,6] console.log(arr);//[1,2,3,4,5,6] 

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.