【问题标题】:Retrieve the value from a 3D array从 3D 数组中检索值
【发布时间】:2021-12-14 03:53:45
【问题描述】:

假设我有一个像这样的 3D 数组

[
  [
    [3.12234, 50.12322],
    [3.12332, 12.12323],
    [3.431232, 122.22317],
  ],
]

我应该如何编写代码来获取这个数组中的任何一个值?

【问题讨论】:

    标签: javascript arrays typescript


    【解决方案1】:

    试试这个:

    var test = [
      [
        [3.12234, 50.12322],
        [3.12332, 12.12323],
        [3.431232, 122.22317],
      ],
    ]
    
    var open = test[0] // get the array
    [2] // get either [3.12234, 50.12322], [3.12332, 12.12323], or [3.431232, 122.22317] 
    [1] // get item [0] or [1] from each value
    console.log(open);
    <body>
    
    </body>

    【讨论】:

      【解决方案2】:

      1) 您可以将数组indexing 用作:

      const arr = [
        [
          [3.12234, 50.12322],
          [3.12332, 12.12323],
          [3.431232, 122.22317],
        ],
      ];
      
      const valueUsingMethod1 = arr[0][0];
      console.log(valueUsingMethod1);

      2)你也可以使用array-destructuring作为

      const arr = [
        [
          [3.12234, 50.12322],
          [3.12332, 12.12323],
          [3.431232, 122.22317],
        ],
      ];
      
      const [[valueUsingMethod2]] = arr;
      console.log(valueUsingMethod2);

      3)你也可以在这里使用flat

      const arr = [
        [
          [3.12234, 50.12322],
          [3.12332, 12.12323],
          [3.431232, 122.22317],
        ],
      ];
      
      const [firstValue] = arr.flat(1);
      console.log(firstValue);

      【讨论】:

        猜你喜欢
        • 2012-01-28
        • 2021-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多