0

I want to create a 2D array, with varying width. I have initialized the outer array as I know the number of rows.

var listofcities = new Array(lengthofcites); //i get this lengthofcitles from another routine

Now, I will get the list of facilities in the cities from another routine as list. E.g. listoffacilities = ["water","air"] for a city. For another city I will get this list as listoffacilities = ["water","air","electricity"].

I should be able to store this in my outer array in such a way that

listofcities[0] = ["water","air"]
listofcities[1] = ["water","air","electricity"]

I'm not able to use the push function like listofcities[0].push(listoffacilities) in the inner loop.

All the examples I could see from web have same sized rows/columns.

4
  • "I'm not able to use the push function..." Why not? Commented Oct 23, 2018 at 15:16
  • What is listofcities? It sounds like you're not initialising it, hence why .push wouldn't work Commented Oct 23, 2018 at 15:18
  • 1
    @George - The op shows it in the question: var listofcities = new Array(lengthofcities);. Commented Oct 23, 2018 at 15:20
  • 1
    @T.J.Crowder My bad, I missed that, thank you; makes sense why listofcities[0].push isn't working then. Commented Oct 23, 2018 at 15:21

2 Answers 2

2

JavaScript doesn't have multi-dimensional arrays; it has arrays of arrays. So multi-dimensional arrays are inherently jagged (the term for when not all subordinate arrays are of the same length).

You do it literally as you've shown:

var listofcities = []; // No need to pre-allocate length
listofcities[0] = ["water","air"];
listofcities[1] = ["water","air","electricity"];
console.log(listofcities);
.as-console-wrapper {
  max-height: 100% !important;
}

This:

var listofcities = [];

creates an empty outer array, and then these:

listofcities[0] = ["water","air"];
listofcities[1] = ["water","air","electricity"];

create subordinate arrays (e.g., ["water","air"]) and store them in that outer array. [...] is an array initializer (often called an "array literal") that creates an array with the items between the brackets.

Here's an example using push; note that you call push on the array (listofcities.push(...)), not an an entry in that array (listofcities[0].push(...)). But once you've put a subordinate array in as an entry, you can push to that subordinate array (listofcities[0].push(...)).

var listofcities = []; // No need to pre-allocate length
listofcities.push(["water","air"]);
listofcities.push(["water","air","electricity"]);
console.log(listofcities);
.as-console-wrapper {
  max-height: 100% !important;
}

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

Comments

0

You shouldn't be pushing onto a specific element of listofcities, just push onto the array itself:

var listofcities = []; // don't specify the length here
listoffacilities = ["water", "air"];
listofcities.push(listoffacilities);
listoffacilities = ["water","air","electricity"];
listofcities.push(listoffacilities);
console.log(listofcities);

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.