-2
var a = ["1", "2"];
var b = ["a", "b"];
var c = [];

//Can someone suggest how do i push a and b into c such that c is a 2-dimensional array? (By using JavaScript)

//c should look like [["1","2"],["a","b"]]

Nikhil S
  • 35
  • 7
  • 1
    `c.push(a, b)`? – Nina Scholz Dec 03 '17 at 11:44
  • 1
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) **Extremely** basic questions are best resolved with research and tutorials, not questions on Stack Overflow. Prior to asking a question on SO, you're expected to do thorough research. – T.J. Crowder Dec 03 '17 at 11:44

2 Answers2

0

You are asking a very basic question, the answer is:

var a = ["1", "2"];
var b = ["a", "b"];
var c = []; // you could have written c = [a, b];

c.push(a, b);
micnic
  • 10,915
  • 5
  • 44
  • 55
  • If i were to do that, c would result in [1,2,a,b] but I want c to be a two-dimensional array. It should be [[1,2],["a","b"]] – Nikhil S Dec 03 '17 at 14:15
0

It's quite simple. Just use push function and pass the array as an argument. c.push(a); c.push(b); or push them together like: c.push(a,b);

  • If i were to do that, c would result in [1,2,a,b] but I want c to be a two-dimensional array. It should be [[1,2],["a","b"]] – Nikhil S Dec 03 '17 at 14:15
  • No, you will get a two-dimensional array. push a and b into c and then console.log the c. You will see that c contains two arrays. –  Dec 03 '17 at 20:45