javascript - 从 3D 数组中检索值
问题描述
假设我有一个像这样的 3D 数组
[
[
[3.12234, 50.12322],
[3.12332, 12.12323],
[3.431232, 122.22317],
],
]
我应该如何编码以获取此数组中的任何一个值?
解决方案
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);
推荐阅读
- xml - XSLT 拆分一个以逗号分隔的长字符串值
- node.js - mocha 和 chai 超过 2000 毫秒的单元测试错误。用于异步测试和钩子
- java - 大型数据集:没有一个配置的节点可用于大型数据集
- python - 如何将 Matlab 中的样条曲线转换为 python?
- kubernetes - 删除命名空间挂起自定义资源终结器
- java - 查找以 1 结尾的输入的下一个素数
- django - 如何将数据传递给基于类的视图方法?
- ios - 如何使用其 Web-API 让 IOS 应用播放 Spotify 播放列表?
- asp.net-mvc - 地图路由不使用默认操作
- swift5 - 'withUnsafeMutableBytes' 已弃用:使用 `withUnsafeMutableBytes