如何在JavaScript中将十进制字符串数组转换为不带十进制的整数字符串数组
我们需要编写一个包含十进制字符串数组的JavaScript函数。该函数应返回一个整数字符串数组,该整数字符串是通过对数组的原始对应十进制值求和而获得的。
例如,如果输入数组为-
const input = ["1.00","-2.5","5.33333","8.984563"];
那么输出应该是-
const output = ["1","-2","5","8"];
示例
为此的代码将是-
const input = ["1.00","-2.5","5.33333","8.984563"];
const roundIntegers = arr => {
const res = [];
arr.forEach((el, ind) => {
const strNum = String(el);
res[ind] = parseInt(strNum);
});
return res;
};
console.log(roundIntegers(input));输出结果
控制台中的输出-
[ 1, -2, 5, 8 ]
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短