RamAlx
RamAlx

Reputation: 7344

Creating a multidimensional array of maps in es6

i have an array of immutable lists array = [List, List] Each list contains Maps. I want to create an array which contains the Maps of each list. My code is the following:

const content = [];
    for (let i = 0; i < types.length; i += 1) {
      let j = 0;
      while (j < types[i].length) {
        content[i] = types[i].get(j);
        j += 1;
      }
    }

But when i console.log this it shows me an empty array. The desired output should be something like this content[0] = [Map, Map, Map, Map] content[1] = [Map, Map, Map, Map]

Upvotes: 0

Views: 596

Answers (1)

guest271314
guest271314

Reputation: 1

The JavaScript in the question is overwriting content[i] in the while loop; content[i] is not an array. Define content[i] as an array before the while loop, and use .push() within the while loop.

const content = [];

for (let i = 0; i < types.length; i += 1) {
  let j = 0;
  content[i] = [];
  while (j < types[i].length) {
    content[i].push(types[i].get(j));
    j += 1;
  }
}

Upvotes: 1

Related Questions