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);
Ok, thank you for your input.. it means that I’ll just make a
function
to place into an array with 4 arguments..col
,row
,arr
&data
With this function, you can take an empty array, place row 10 col 4 in empty array putting any data like ‘xD’ and it will work out eg: try
place2d(10,4,emptyArrName,"xD")
and it works.. just one thing to note..IT ONLY APPLIES array structures to WHERE IT NEEDS TO.. doing things like the example above would leave a lot of undefined slots.. wild example below