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?

Vijay
3,0231 gold badge18 silver badges24 bronze badges
asked Feb 1, 2015 at 4:41
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/

answered Feb 1, 2015 at 4:47
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]
answered Feb 1, 2015 at 5:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.