I am in a function where i fetch a multidimensional array from the parent container. The array fetched doesn't always have the same dimensions (it can be 1D, 2D, 3D, 4D, ...).
I have for parameter a 1D array containing the coordinates, and the value.
function(coordinates_array, value) {
var muti_dim_array = getArrayByName(another_param);
}
Unfortunately, i can't do
multi_dim_array[coordinates_array[0]][coordinates_array[1]][...]
because the dimensions are not always the same.
I could do a switch case with the length of the coordinates_array, but that would be very bad since my multi_dim_arrays can be between 1D and 10D.
What i tried :
function(coordinates_array, value) {
var multi_dim_array = getArrayByName(another_param);
//Transform "[1, 2, 3]" in "[1][2][3]"
var coord = JSON.stringify(coordinates_array).replace(/,/g, '][');
var array_value = eval('multi_dim_array'+coord);
array_value = value;
}
But it doesn't work since
multi_dim_array[1][2][3]
isn't updated.
And this :
eval('multi_dim_array'+coord) = value;
doesn't work either.
How to update in a generic way a multidimentional array (with different dimensions), given the coordinates to modify in a 1d array, and the new value ?
Examples :
I can have as parameter
coordinates_array = [1, 5, 6, 7]
coordinates_array = [2, 3]
coordinates_array = [8]
And i want to do
multi_dim_array[1][5][6][7] = value
multi_dim_array[2][3] = value
multi_dim_array[8] = value
All that in the least lines of code possible (no switch case checking the coordinates_array length).