cari

Rumah  >  Soal Jawab  >  teks badan

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

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

怪我咯怪我咯2818 hari yang lalu384

membalas semua(4)saya akan balas

  • 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);});
    });

    balas
    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);
    

    balas
    0
  • 怪我咯

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

    妥善的转换方式显然是 parseInt

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

    balas
    0
  • 大家讲道理

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

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

    //其实还是循环

    balas
    0
  • Batalbalas