JS multidimensional array modify value

时光总嘲笑我的痴心妄想 提交于 2019-12-25 03:35:43

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/33755505/js-multidimensional-array-modify-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!