0

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).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bad Wolf
  • 81
  • 6

1 Answers1

0

I have found a solution :

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, '][');

    eval('multi_dim_array' + coord + ' = ' + value);
}

But i'm not quite satisfied with it. Can anyone find a better solution ?

Or can anyone tell me if my approach is good performance wise. I'll call this method very often : more than 700 multidimentional arrays in total, and they can all be uptated very often (like when i resize a div, i need to update 4 different arrays (top, left, width, height), ...).


After several perf tests, I decided to go for an 'ugly' switch case :

function(coordinates_array, value) {
    var multi_dim_array = getArrayByName(another_param);

    switch(coordinates_array.length) {
        case 1: multi_dim_array[coordinates_array[0]] = value;break;
        case 2: multi_dim_array[coordinates_array[0]][coordinates_array[1]] = value;break;
        case 3: multi_dim_array[coordinates_array[0]][coordinates_array[1]][coordinates_array[2]] = value;break;
        case.....
    }
}

Indeed, for a simple calling test over 100 000 iterations, the switch case solution revealed to be over 500x faster than the eval() one (10ms to complete the switch case vs over 5000ms to complete the eval test).

If anyone finds a cleaner solution than the switch case one, i'm still interested, even if it's a little bit less performant (not more than ~2x ; 3x times though).

Bad Wolf
  • 81
  • 6