首页 > 解决方案 > 具有自定义应用函数的节点 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_strmax_str

标签: javascript

解决方案


您可以在破坏之前映射数据

const [min, max] = data.map(d => parseInt(d, 10));

推荐阅读