【问题标题】:array index selection like in numpy but in javascript数组索引选择,如 numpy 但在 javascript 中
【发布时间】:2019-03-11 05:48:51
【问题描述】:

我有一个 3x3 数组:

var my_array = [[0,1,2],
                [3,4,5],
                [6,7,8]];

并希望获得它的第一个 2x2 块(或任何其他 2x2 块):

[[0,1], 
 [3,4]]

用 numpy 我会写:

my_array = np.arange(9).reshape((3,3))
my_array[:2, :2]

得到正确的结果。

我在 javascript 中尝试过:

my_array.slice(0, 2).slice(0, 2);

但是第二个切片影响第一个维度,什么都不做。 我注定要使用 for 循环,还是有一些新的 ES6 语法可以让我的生活更简单?

【问题讨论】:

    标签: javascript arrays numpy multidimensional-array indexing


    【解决方案1】:

    可以使用Array.sliceArray.map 的组合:

    const input = [
      [0, 1, 2],
      [3, 4, 5],
      [6, 7, 8]
    ];
    
    const result = input.slice(0, 2).map(arr => arr.slice(0, 2));
    
    console.log(result);

    【讨论】:

      【解决方案2】:

      您可以使用.map().slice() 方法:

      var my_array = [[0,1,2],
                      [3,4,5],
                      [6,7,8]];
                      
      var result = my_array.slice(0, 2).map(a => a.slice(0, 2));
      
      console.log(result);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        【解决方案3】:

        您可以将对象作为索引和所需子数组的长度。然后对切片的子数组进行切片和映射。

        var array = [[0, 1, 2], [3, 4, 5], [6, 7, 8]],
            length = { x: 2, y: 2 },
            indices = { i: 0, j: 0 },
            result = array.slice(indices.i, indices.i + length.x).map(a => a.slice(indices.j, indices.j + length.y));
            
        console.log(result);

        【讨论】:

          【解决方案4】:

          它似乎不是一个 3x# 数组,它只是数组的数组。

          你可以先用 slice 得到一个只有前两个元素的数组

          [[0, 1, 2],[3, 4, 5]]
          

          然后使用reduce返回一个新数组&里面只得到前两个值

          var my_array = [
            [0, 1, 2],
            [3, 4, 5],
            [6, 7, 8]
          ];
          
          let x = my_array.slice(0, 2).reduce(function(acc, curr) {
            acc.push(curr.slice(0, 2))
            return acc;
          }, [])
          console.log(x)

          【讨论】:

            【解决方案5】:

            const input = [
              [0, 1, 2],
              [3, 4, 5],
              [6, 7, 8]
            ];
            let result =[]
            input.forEach(([a, b], index) => {if(index < 2) result.push([a, b])})
            
            console.log(result);

            【讨论】:

              【解决方案6】:

              如果您要大量使用矩阵,那么您应该查看math.js

              尝试以下方法:

              var my_array = [[0,1,2],
                              [3,4,5],
                              [6,7,8]];
              
              var matrix = math.matrix(my_array);
              
              var subset = matrix.subset(math.index([0, 1], [0, 1]));
              

              工作example

              【讨论】:

              • 我读到它很慢,不是很慢,但比自己做的要慢。是真的吗?
              • 我没有关于它的性能的记录,但我认为它应该不会慢。
              猜你喜欢
              • 1970-01-01
              • 2021-07-20
              • 1970-01-01
              • 2018-08-26
              • 2014-04-21
              • 1970-01-01
              • 1970-01-01
              • 2020-10-27
              • 1970-01-01
              相关资源
              最近更新 更多