javascript - 对于数组中的每个数组
问题描述
我有这样的数组:
[[[37.1316, 56.01645], [37.15117, 55.99955], [37.14439, 55.98932], [37.14661, 55.96372], [37.18212, 55.95873], [37.20565, 55.96284], [37.21284, 55.9766], [37.26641, 55.97947], [37.26056, 55.99314], [37.18898, 56.02122], [37.16173, 56.01322], [37.1316, 56.01645]]]
如何使用 toFixed 获取数组 toFixed(2) 中的每个项目?
在输出上我需要这样:
[[[37.73, 55.87], [37.69, 55.89], [37.63, 55.89], [37.58, 55.91], [37.55, 55.90], [37.56, 55.94], [37.51, 55.94], [37.53, 55.90], [37.56, 55.89], [37.57, 55.86], [37.57, 55.83], [37.57, 55.82], [37.58, 55.79], [37.65, 55.78], [37.65, 55.80], [37.66, 55.82], [37.67, 55.83], [37.69, 55.84], [37.72, 55.85], [37.70, 55.86], [37.73, 55.87]]]
解决方案
用于Array.prototype.map
映射内部数组,并用于Number.prototype.toFixed
您获得的各个解构浮点数。
用于Array.prototype.reduce
累加运算的处理结果:
const arr = [
[
[37.1316, 56.01645],
[37.15117, 55.99955],
[37.14439, 55.98932],
[37.14661, 55.96372],
[37.18212, 55.95873],
[37.20565, 55.96284],
[37.21284, 55.9766],
[37.26641, 55.97947],
[37.26056, 55.99314],
[37.18898, 56.02122],
[37.16173, 56.01322],
[37.1316, 56.01645]
]
];
const data = arr.reduce((acc, innerArray) => {
const t = innerArray.map(
([first, second]) => [first.toFixed(2), second.toFixed(2)]
);
acc.push(t);
return acc;
}, []);
console.log(data);
推荐阅读
- unix - 在 MakeFile 中传递带有间隙的字符串
- python - 使用 __iter__ 和 __next__ 创建我自己的类
- android - Android - 在用户触摸时在应用程序中显示/隐藏工具栏,并带有向上和向下滑动动画
- github - 通过 ssh 连接到 github 存储库
- azure - 如何将 Composer 上的 Created BOT 发布到 Azure 并使其可供最终用户使用?
- python - 如何自定义具有模糊效果的 Qbrushstyle
- pandas - 带有 iloc 的 Pandas_UDF 连接循环
- angular - 如何在Angular中测试组件时正确指定哪个组件具有泛型类型的输入数据
- oracle - 如何在不将每个表单独拖动到关系模型窗口的情况下使用更改更新我的 ERD?
- css - 试图在整页背景图像中将徽标居中,但导航栏将其向下推