0

I’m trying to combine 3 arrays into one "2d array":

resultArr = [
  ["abc", 0, []],
  ["def", 0, []],
  ["ghi", 0, []],
  ["jkl", 0, []],
  ["mno", 0, []],
  ["pqr", 0, []],
  ["stu", 0, []]
];

Basically, I’m starting with an arbitrary array as my input, such as snippets:

Array 1:

const snippets = ['abc', "def", "ghi", "jkl", "mno", "pqr", "stu"];

The next steps was easy, I created a an array filled with zeros:

Array 2:

const zeros = new Array(snippets.length).fill(0); // [0, 0, 0, 0, 0, 0, 0]

Now, I tried the same to get a list of empty arrays:

Array 3:

const zeroArr = new Array(snippets.length).fill([]); // → circular object arrays :(

Obviously, fill cannot handle nested arrays .fill([]) or .fill([[]]). Is there any way to create this nested array structure?

FYI: I continue to combine these rows like so, it works if I would leave out the last , zeroArr[0][i]] part:

  let combined = [];
  for (let i = 0; i < genreLength; i++) {
    combined.push([snippets[i], zeros[i], zeroArr[0][i]]);
  }
Kalaschnik
  • 769
  • 1
  • 7
  • 21

0 Answers0