简体   繁体   English

在Javascript中读取带有数组的数组?

[英]Read an array with an array in Javascript?

If a=[[1,[],"f",3],[3,[4,"x"]]] and b=[1,1] . 如果a=[[1,[],"f",3],[3,[4,"x"]]]b=[1,1]

I want to read a by b like a[1][1] to get [4,"x"] . 我想读aba[1][1]得到[4,"x"] Note that b is an array which should only consist of integers. 请注意, b是一个只应由整数组成的数组。

You could also do eval('a['+b.join('],[')+']') but requires the actual variable name as string and it's ugly. 你也可以做eval('a['+b.join('],[')+']')但是需要实际的变量名作为字符串而且它很难看。

Here are some of my functions: 以下是我的一些功能:

Array.prototype.readByArray = function(a) {
    var c = this;
    for (var i = 0; i < a.length; i++) {
        c = c[a[i]];
    }
    return c;
};
Array.prototype.emptyByArray = function(a) {
    var c = this.readByArray(a);
    c.splice(0, c.length);
};
Array.prototype.concateByArray = function(a, e) {
    var c = this.readByArray(a);
    for (var i = 0; i < e.length; i++) {
        c.push(e[i]);
    }
};
Array.prototype.setByArray = function(a, e) {
    this.emptyByArray(a);
    this.readByArray(a).push(e);
};

This could be useful for reading a nested array in an imperative way in this example: 在此示例中,这对于以命令方式读取嵌套数组非常有用:

Array.prototype.readByArray=function(a){var c=this;for(var i=0;i<a.length;i++){c=c[a[i]];}return c;};
var a = [1,2,3,[1,2,3,[{x: 3},"test"],4],"foo","bar"]; //Your array
var b = [0]; //Reading stack
var s = '[\n'; //Output
while(b[0]<a.length){
    if(Array.isArray(a.readByArray(b))){
        s+=' '.repeat(b.length)+'[\n';
        b.push(-1);
    }else{
        s+=' '.repeat(b.length)+JSON.stringify(a.readByArray(b))+'\n';
    }
    b[b.length-1]++;
    while(b[b.length-1]>=a.readByArray(b.slice(0,-1)).length){
        b.pop();
        b[b.length-1]++;
        s+=' '.repeat(b.length)+']\n';
    }
}
console.log(s);

Is there any better way to do this? 有没有更好的方法来做到这一点? Are there native functions for this? 这有本机功能吗?

You could use Array#reduce for it. 您可以使用Array#reduce

You start with the whole array and return for every element of b a part of the array until all indices are used. 您从整个数组开始,并返回数组的一部分b的每个元素,直到使用所有索引。

 var a = [[1, [], "f", 3], [3, [4, "x"]]], b = [1, 1], result = b.reduce(function (v, i) { return v[i]; }, a); console.log(result); 

ES6 ES6

 var a = [[1, [], "f", 3], [3, [4, "x"]]], b = [1, 1], result = b.reduce((v, i) => v[i], a); console.log(result); result[0] = 42; console.log(a); result.splice(0, result.length, 'test'); console.log(a); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

I had written a reusable generic code exactly for this purpose to get the nested object properties dynamically. 我为此目的编写了一个可重用的通用代码,以动态获取嵌套对象属性。 Actually i was targeting objects but since in JS an array is a perfect object it also applies for arrays too. 实际上我是针对对象,但因为在JS中数组是一个完美的对象,它也适用于数组。 So lets see how it works in this particular case; 所以让我们看看它在这种特殊情况下的工作原理;

 Object.prototype.getNestedValue = function(...a) { return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]]; }; var a = [[1,[],"f",3],[3,[4,"x"]]], b = [1,1], c = a.getNestedValue(...b); console.log(c) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM