multidimensional index by array of indices in JavaScript

回眸只為那壹抹淺笑 提交于 2020-01-05 04:14:17

问题


Is there a way in JavaScript to select a element of a multidimential array. Where the depth/rank/dimensionality is variable and the keys are given by a array of indices. Such that i don't have handle every possible dimentional depth separately. concretely speaking i want do get rid of switch cases like here:

/**
 * set tensor value by index
 * @type {array} indices [ index1, index2, index3 ] -> length == rank.
 * @type {string} value.
 */
tensor.prototype.setValueByIndex = function( indices, value ) {
    var rank = indices.length;

    switch(rank) {
        case 0:
            this.values[0] = value;
        break;
        case 1:
            this.values[indices[0]] = value;
        break;
        case 2:
            this.values[indices[0]][indices[1]] = value;
        break;
        case 3:
            this.values[indices[0]][indices[1]][indices[2]] = value;
        break;
    }
}

Where this.values is a multidimensional array.

such that i get something that looks more like this:

/**
 * set tensor value by index
 * @type {array} indices, [ index1, index2, index3 ] -> length == rank
 * @type {string} value
 */
tensor.prototype.setValueByIndex = function( indices, value ) {
    var rank = indices.length;

    this.values[ indices ] = value;
}

Thank you in advance!


回答1:


tensor.prototype.setValueByIndex = function( indices, value ) {
    var array = this.values;
    for (var i = 0; i < indices.length - 1; ++i) {
        array = array[indices[i]];
    }
    array[indices[i]] = value;
}

This uses array to point to the nested array we are currently at and reads through the indicies for find the next array value from the current array. Once we reach the last index in the indices list, we have found the array where we want to deposit the value. The final index is the slot in that final array where we deposit the value.




回答2:


Something like this:

tensor.prototype.setValueByIndex = function( indexes, value ) {
    var ref = this.values;  
    if (!indexes.length) indexes = [0];  
    for (var i = 0; i<indexes.length;i++) {
       if (typeof ref[i] === 'undefined') ref[i] = [];
       if (ref[i] instanceof Array) {  
           ref = ref[i];
       } else {
           throw Error('There is already value stored') 
       }
    } 
    ref = value;
}



回答3:


Why would you want to do that? I'd say writing

tensor.values[1][5][8][2] = value;

is much more perspicuous than

tensor.setValues([1, 5, 8, 2], value);

If you really need to do that, it would be a simple loop over the array:

tensor.prototype.setValueByIndex = function(indices, value) {
    var arr = this.values;
    for (var i=0; i<indices.length-1 && arr; i++)
        arr = arr[indices[i]];
    if (arr)
        arr[indices[i]] = value;
    else
        throw new Error("Tensor.setValueByIndex: Some index pointed to a nonexisting array");
};



回答4:


Like this?

tensor.prototype.setValueByIndex = function( indices, value ) {
  var t = this, i;
  for (i = 0; i < indices.length - 1; i++) t = t[indices[i]];
  t[indices[i]] = value;
}


来源:https://stackoverflow.com/questions/11019269/multidimensional-index-by-array-of-indices-in-javascript

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