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;
}
先感谢您!