Let's say I have an empty 1D array:
var data = [];
Now, I want to add value of 1 to data[1][1][3];
To do this, I need to extend data array to:
data = [
[],
[],
[[],[],[1]]
]
Yeah, pretty sure that I have to write a function and pass dimension values as 1D array [1, 1, 3] and check 1. if there is second, third dimension and if 'no' create them and 2. if its size are greater or equal.
For just this example, function would be
function setData(value_, index_){
if(data[index_[0]] == undefined){
data = Array(index_[0] + 1).fill([]);
data[index_[0]] = Array(index_[1] + 1).fill(1);
data[index_[0]][index_[1]] = Array(index_[2] + 1).fill([]);
data[index_[0]][index_[1]][index_[2]] = value_;
}else{
if(data[index_[0]][index_[1]] == undefined){
data[index_[0]][index_[1]] = Array(index_[2] + 1).fill([]);
data[index_[0]][index_[1]][index_[2]] = value_;
}
}
}
It's clumsy and straight-forward. How I can make an universal thing out of it? For any # of dimensions.
var data = [];
setData(false, [1, 1, 3]);
function setData(value_, index_){
if(data[index_[0]] == undefined){
data = Array(index_[0] + 1).fill([]);
data[index_[0]] = Array(index_[1] + 1).fill(1);
data[index_[0]][index_[1]] = Array(index_[2] + 1).fill([]);
data[index_[0]][index_[1]][index_[2]] = value_;
}else{
if(data[index_[0]][index_[1]] == undefined){
data[index_[0]][index_[1]] = Array(index_[2] + 1).fill([]);
data[index_[0]][index_[1]][index_[2]] = value_;
}
}
}
console.log(data);