2

So i have 2 single dim arrays

array1 = [1,2,3]; array2 = [4,5,6];

And an empty 2-dim array

setArray = [[],[]];

How am i going to push the contents of the arrays to setArray so that it looks like this... considering we don't know the length of the single dim arrays?

setArray = [
 [1,2,3],
 [4,5,6]
];

I'm working on a project and my problem looks like this. Thank you.

2
  • 4
    setArray = [array1,array2]; Commented Jul 13, 2017 at 6:56
  • Can you post a bit more of your code, it's kinda hard what you're actually trying to do. I get you're trying to create a 2 dimensional array, but how are you trying to do it? Commented Jul 13, 2017 at 6:59

2 Answers 2

2

if you just want to push contents of the two arrays in setArray you can do something like this: Each element of setArray will have a reference to one of the arrays.

   
var array1 = [1,2,3], array2 = [4,5,6];
var setArray = [];

setArray.push(array1);
setArray.push(array2);
console.log(setArray);

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

Comments

1

array1 = [1,2,3]; array2 = [4,5,6];
setArray = [array1,array2];

console.log(setArray)

You can do it this way too.

1 Comment

Thanks for upvote. If it helped you, kindly accept this as answer.

Your Answer

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.