Index an array of arrays with an array of indexes in javascript

前端 未结 1 502
孤街浪徒
孤街浪徒 2020-12-22 09:15

Given an array of arrays [of arrays ...] such as:

array[a][b][c][d][e]

And an array of indexes:

indexes = [ a, b, c, d, e ]         


        
相关标签:
1条回答
  • 2020-12-22 09:54

    You can use Array.reduce() to go through the indexes and get the element you want.

    // array[a][b][c][d][e]
    var indexes = [ a, b, c, d, e ];
    
    var element = indexes.reduce(function(prev, curr){
        return prev[curr];
    }, array);
    
    0 讨论(0)
提交回复
热议问题