首页 > 解决方案 > 从 3D 数组中检索值

问题描述

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

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

我应该如何编码以获取此数组中的任何一个值?

标签: javascriptarraystypescript

解决方案


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);


推荐阅读