【问题标题】:multidimensional index by array of indices in JavaScriptJavaScript中索引数组的多维索引
【发布时间】:2012-06-13 16:27:26
【问题描述】:

在 JavaScript 中有没有一种方法可以选择多维数组的元素。其中深度/等级/维度是可变的,键由索引数组给出。这样我就没有分别处理每个可能的维度深度。具体来说,我想摆脱像这里这样的开关盒:

/**
 * 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;
    }
}

其中 this.values 是一个多维数组。

这样我得到的东西看起来更像这样:

/**
 * 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;
}

提前谢谢你!

【问题讨论】:

标签: javascript arrays indexing switch-statement multidimensional-array


【解决方案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;
}

这使用array 指向我们当前所在的嵌套数组,并读取indicies 以从当前array 中找到下一个array 值。一旦我们到达indices 列表中的最后一个索引,我们就找到了要存放值的数组。最终索引是我们存放值的最终数组中的槽。

【讨论】:

    【解决方案2】:

    类似这样的:

    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】:

      您为什么要这样做?我会说写作

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

      比

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

      如果你真的需要这样做,这将是一个简单的数组循环:

      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");
      };
      

      【讨论】:

      • 是的,它看起来微不足道,但是。我想使用变量“索引数”来创建一个抽象张量,使秩等于“索引数”。这样所有操作都可以以抽象的方式定义,而不是为不同的等级和维度单独定义所有操作
      • 感谢您的解决方案。事实证明这很简单:P,谢谢!。
      【解决方案4】:

      像这样?

      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;
      }
      

      【讨论】:

      • 不,他想要一个 set 而不是 get
      猜你喜欢
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      相关资源
      最近更新 更多