在JavaScript的数组索引多维指数 [英] multidimensional index by array of indices in JavaScript

查看:117
本文介绍了在JavaScript的数组索引多维指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个JavaScript的方式来选择multidimential数组的元素。其中深度/秩/维数是可变的,所述密钥由索引的阵列给出。这样我不必单独处理每一个可能的三维深度。具体来说,我想办摆脱开关情况下,像在这里:

  / **
 *通过索引设定值张
 * @type {}数组索引[索引1,索引2,INDEX3] - >长度==军衔。
 * @type {}的字符串值。
 * /
tensor.prototype.setValueByIndex =功能(指数值){
    VAR等级= indices.length;    开关(秩){
        情况下0:
            this.values​​ [0] =价值;
        打破;
        情况1:
            this.values​​ [指数为[0]] =值;
        打破;
        案例2:
            this.values​​ [指数为[0] [指数[1] =值;
        打破;
        案例3:
            this.values​​ [指数为[0] [指数[1]] [指数[2] =值;
        打破;
    }
}

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

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

  / **
 *通过索引设定值张
 * @type {}数组索引[索引1,索引2,INDEX3] - >长度==排名
 * @type {}的字符串值
 * /
tensor.prototype.setValueByIndex =功能(指数值){
    VAR等级= indices.length;    this.values​​ [指数] =价值;
}

感谢你在前进!


解决方案

  tensor.prototype.setValueByIndex =功能(指数值){
    VAR阵列= this.values​​;
    对于(VAR I = 0; I< indices.length - 1 ++ I){
        阵列=阵列[指数[I]];
    }
    数组[索引[I] =价值;
}

本使用阵列指向嵌套数组,我们目前并通过 indicies读为找到从目前的阵列下一阵列。一旦我们达到了指数列表中的最后一个索引,我们已经发现,我们想存入的值的数组。最终的指数是在最后的数组,其中存放我们的价值插槽。

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!

解决方案

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.

这篇关于在JavaScript的数组索引多维指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆