6

I'm trying to make a list of arrays in JAVASCRIPT but my program doesn't do what I expect.

I want to make a list of arrays like this:

var myListofArrays;

var firstArray = [1,2,3,4,5,6];
var secondArray = [6,5,4,3,2,1];
var thirdArray = [1,1,1];

I want to add those arrays in myListArray to finally get something like that:

myListofArrays[0] = [1,2,3,4,5,6];
myListofArrays[1] = [6,5,4,3,2,1];
myListofArrays[2] = [1,1,1];

Anyone knows how to do it?

Thank you very much!

2
  • 2
    myListOfArrays is undefined. Initialize it with = []. Or just do it all in one nested literal var myListOfArrays = [ [1,2,3,4,5,6], [6,5,4,3,2,1], [1,1,1] ]; Commented Jul 4, 2016 at 21:24
  • 1
    Also should see that it is undefined by looking at error thrown in browser console...big clue there Commented Jul 4, 2016 at 21:26

5 Answers 5

11

Starting from:

var firstArray = [1,2,3,4,5,6];
var secondArray = [6,5,4,3,2,1];
var thirdArray = [1,1,1];

You only need create a new array object:

var myListOfArrays = [];

and then, insert all arrays that you want into the new array:

mylistOfArrays.push(firstArray);
mylistOfArrays.push(secondArray);
mylistOfArrays.push(thirdArray);

With push() method the array object is added at end of the new array. This method also returns the new length.

You can visit this reference to get a detailed list of array methods: Array Methods

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

1 Comment

there is a typo: mylistOfArrays.push should be myListOfArrays.push
5

There are many ways to do this. Try:

var myListofArrays = [];

var firstArray = [1,2,3,4,5,6];
var secondArray = [6,5,4,3,2,1];
var thirdArray = [1,1,1];

myListofArrays.push(firstArray);
myListofArrays.push(secondArray);
myListofArrays.push(thirdArray);

Or a little fancier:

myListofArrays.push(firstArray, secondArray, thirdArray);

Or:

var firstArray = [1,2,3,4,5,6],
    secondArray = [6,5,4,3,2,1],
    thirdArray = [1,1,1],
    myListOfArrays = [firstArray, secondArray, thirdArray];

And there are still probably plenty other valid ways to do this.

Comments

2

Beside Andrew Clavin's answer you can also try this:

var myListofArrays =[];

var firstArray = [1,2,3,4,5,6];
var secondArray = [6,5,4,3,2,1];
var thirdArray = [1,1,1];

myListofArrays[0]=firstArray;
myListofArrays[1]=secondArray;
myListofArrays[2]=thirdArray;

console.log(myListofArrays);

check it in jsFiddle

1 Comment

@squint Yes you are right, before I want to post the complete answer Andrew Calvin posted it.
1

Hi I think you are almost there.

Try this:

var myListofArrays = [];

You need to declare your variable as an array before you start dumping things in it.

EDIT

If you want to shorten down your code, you could also try this:

var myListofArrays = [];

var firstArray = [1,2,3,4,5,6];
var secondArray = [6,5,4,3,2,1];
var thirdArray = [1,1,1];

myListofArrays = [firstArray, secondArray, thirdArray];

This will push all of your declared arrays into your master array(myListofArrays) and because you declared it as an array at the top, the javascript will understand it is an array and push the rest of the arrays in accordingly.

Comments

0

Since no one mentioned the concat method. Here is my contribution:

var myListofArrays = [];

var firstArray = [1, 2, 3, 4, 5, 6];
var secondArray = [6, 5, 4, 3, 2, 1];
var thirdArray = [1, 1, 1];

function concatAllArrays() {
  for (var i = 0; i < arguments.length; i++) {
    if (!Array.isArray(myListofArrays[i])) {
      myListofArrays[i] = [];
    }
    myListofArrays[i] = myListofArrays[i].concat(arguments[i]);
  }
}

concatAllArrays(firstArray, secondArray, thirdArray);
console.log('My array => ', myListofArrays);

Comments

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.