javascript - 具有自定义应用函数的节点 javascript 数组解构
问题描述
我有一个包含两个字符串的数组:例如“1”和“5”。我想将这两个字符串保存到参数中,但已经保存为整数。目前,代码如下所示:
const data = ['1', '5'];
const [min_str, max_str] = data;
const min = parseInt(min_str, 10);
const max = parseInt(max_str, 10);
console.log(typeof min_str, typeof max_str, typeof min, typeof max)
有没有办法将此代码减少到一行,这样parseInt已经应用于min_str和max_str?
解决方案
您可以在破坏之前映射数据
const [min, max] = data.map(d => parseInt(d, 10));
推荐阅读
- admob - GDPR - UMP SDK:将同意结果转发给调解合作伙伴
- python - 用于与数据库无关的应用程序的 Python 数据库 API VS SQLAlchemy
- javascript - 在 JavaScript 中使用 mouseover 事件中一一切换 li 列表中的类
- postgresql - PostgreSQL:如何在特定 SCHEMA 中授予 GRANT 权限
- cypress - 从命令中使用 Cypress 变量
- json - 使用 JSON 请求 API 的嵌套 Solr 空间查询
- php - crm链接查询表
- javascript - 使用curl时,某些页面的代码被剪切或显示页面的源代码
- ios - 在 iPhone 上播放音乐时录制视频颤动
- visual-studio - Visual Studio 中的 Bit Bucket 登录错误“禁止操作”