Add an array inside another already created Javascript

Asked

Viewed 837 times

-1

I can create an array within another as follows:

var meuArray = [["1","2","3"],["4","5","6"]];

With this I can easily check the positions just by calling meuArray[x][y], where x is which array I want and y which position, for example the number two corresponds to meuArray[0][1], or the number 6 corresponds to meuArray[1][2].

So far so good, now how do I add one more array within my array? for example I have the following array:

var meuSegundoArray = ["7","8","9"];

And I want to add it to my:

meuArray=[["1","2","3"],["4","5","6"],["7","8","9"]];

I’ve tried to do:

meuArray += meuSegundoArray

did not work, as far as push.

Does anyone have any solution to this problem?

2 answers

2


It’s very simple, use the push to do this:

var meuArray = [["1","2","3"],["4","5","6"]];
var meuSegundoArray = ["7","8","9"];

meuArray.push(meuSegundoArray);
  • Show!! I had tested with push again and it did not work, I do not know the reason, but I tested here and it worked right, thanks Douglas!!

0

// Try to use the Concat

var array1 = ['1','2','3'] var array2 = ['4','5','6'] var array3 = ['7','8','9']

var arrayTudo = array1.Concat(array2, array3)

console.log(array)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.