如何在 JavaScript 中将十进制字符串数组转换为无小数的整数字符串数组
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/18 8:44:17
我们需要编写一个 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 ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

