当前位置:网站首页>去掉数组中是数字的引号
去掉数组中是数字的引号
2022-07-19 07:47:00 【new Promise】
今天敲代码的时候想到的问题: 当使用
split()
函数将字符串转换成数组时,里面的数字也会变成string
类型。let str = 'abc123abc123'; let str1= str1.split(''); console.log(str1); //会打印 ['a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3']
可以调用map
函数去掉数组中是数字的引号,使其变成number
类型。
let str = 'abc123abc123';
let str2 = str.split('');
let newStr = str2.map(item => {
// 判断 将该项转换成数组之后不等于NaN, 就进行转换操作
if (!isNaN(item - 0)) {
// 使用+ - 等数学符号可以转换成Number数据类型
return item - 0;
} else {
return item;
}
});
console.log(newStr); // 打印(12) ['a', 'b', 'c', 1, 2, 3, 'a', 'b', 'c', 1, 2, 3]
边栏推荐
猜你喜欢
随机推荐
Appium自动化测试基础 — 操作微信小程序
Shell function array job
MySQL learning notes - stored procedures and functions
1339:求后序遍历
Fractional PID control and vector control permanent magnet motor
Échec de l'enregistrement des intergiciels personnalisés
The Permutation Results by backtracking method (dfs)
ES6新增(一)let与常量
Software testing - learning notes 4
将Float类型的数字保留2位小数
Reptile exercises (III)
What is reverse proxy?
如何来反向论证产品的好坏?
源码编译安装LAMP
Resolved (selenium reports an error) attributeerror: 'webdriver' object has no attribute 'execute_ cdp_ cmd‘
How to demonstrate the quality of products in reverse?
redis电商秒杀设计
Flask请求数据和获取响应
Movinets series models are good helpers for real-time classified videos on mobile phones
Favorite address and historical address of 360 browser