search

Home  >  Q&A  >  body text

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

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

怪我咯怪我咯2817 days ago380

reply all(4)I'll reply

  • ringa_lee

    ringa_lee2017-04-10 15:31:12

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-10 15:31:12

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

    reply
    0
  • 怪我咯

    怪我咯2017-04-10 15:31:12

    妥善的转换方式显然是 parseInt

    console.log( + "100rmb" ) // NaN
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-10 15:31:12

    a.reduce(function(prev, a){ prev.push(+a); return prev;}, []);
    

    //其实还是循环

    reply
    0
  • Cancelreply