Add values into 2D dimensional array in JS

I’m trying to add values into 2D array based on the position that is present in the input data.

For example, the below format represents 0 as row, 0 as column and 5 is length of the value.

 [
    "0,0,5",
    "hello"
 ],

How do I insert the values into 2D array based on position like [0][0], [0][1] and [0][2]?

Except from my code

const data = [
  [
    "0,0,5",
    "hello"
  ],
  [
    "0,1,10",
    "js is fun!"
  ],
  [
    "0,2,0",
    ""
  ]
]

let array2D = [
  []
];
let i = 0
for (let r = 0; r < data.length; ++r) {
  array2D[r] = [];
  for (let c = 0; c < data.length; ++c) {
    array2D[r][c] = data[i++];
  }
}
console.log(array2D);

108 thoughts on “Add values into 2D dimensional array in JS”