如何用map把数组里的string变成number

人找找
  • 28

一个数组["125","445","22","45","68","32","98"],想要变成[125,445,22,45,68,32,98],不用for循环,怎么做

回复
阅读 4.8k
4 个回答

(function(){
var arr = ["125","445","22","45","68","32","98"];
arr = arr.map(function(el){return parseInt(el);});
});

php//其实也是循环
var arr=["125","445","22","45","68","32","98"].map(function(i){
 return +i;
})
console.log(arr);
a.reduce(function(prev, a){ prev.push(+a); return prev;}, []);

//其实还是循环

妥善的转换方式显然是 parseInt

console.log( + "100rmb" ) // NaN
宣传栏