0

In my javascript function i have two single array like this:

var row = ["var1","var2","var3"];
var col = ["res1","res2","res3"];

i would create a multidimensional array like:

[["var1","res1"],["var2","res2"],["var3","res3"]]

i tried:

new Array(m).fill(new Array(n).fill(0));

or solution like:

var dict = [[]];
for (i = 0; i < descendents.length; i++) {
    e = descendents[i];
    dict[i] = dict[i][e.id]
    dict[i] = dict[i][e.value]
}

but the result is not correct for me. i don't know how achieve this

Thanks in advance

Manuel Santi
  • 1,106
  • 17
  • 46

1 Answers1

0

Use Array#map to generate a new array based on existing.

var row = ["var1", "var2", "var3"];
var col = ["res1", "res2", "res3"];
 
// iterate and generate new array based on row array element
// and fetch element from col using the same index
var res = row.map((v, i) => [v, col[i]]);

console.log(res)

Or with Array.from with a map function.

var row = ["var1", "var2", "var3"];
var col = ["res1", "res2", "res3"];

var res = Array.from({ length: row.length }, (_, i) => [row[i], col[i]]);

console.log(res)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188