How to convert all elements in an array to integer in JavaScript?

前端 未结 11 1455
生来不讨喜
生来不讨喜 2020-12-07 14:47

I am getting an array after some manipulation. I need to convert all array values as integers.

My sample code

var result_string = \'         


        
相关标签:
11条回答
  • 2020-12-07 15:20
    var inp=readLine();//reading the input as one line string
    var nums=inp.split(" ").map(Number);//making an array of numbers
    console.log(nums);`
    

    input : 1 9 0 65 5 7 output:[ 1, 9, 0, 65, 5, 7 ]

    what if we dont use .map(Number)

    code

    var inp=readLine();//reading the input as one line string
    var nums=inp.split(" ");//making an array of strings
    console.log(nums);
    

    input : 1 9 0 65 5 7 output:[ '1', '9', '0', '65', '5', '7']

    0 讨论(0)
  • 2020-12-07 15:21

    Just loop the array and convert items:

    for(var i=0, len=count_array.length; i<len; i++){
        count_array[i] = parseInt(count_array[i], 10);
    }
    

    Don't forget the second argument for parseInt.

    0 讨论(0)
  • 2020-12-07 15:21

    The point against parseInt-approach:

    There's no need to use lambdas and/or give radix parameter to parseInt, just use parseFloat or Number instead.


    Reasons:

    1. It's working:

      var src = "1,2,5,4,3";
      var ids = src.split(',').map(parseFloat); // [1, 2, 5, 4, 3]
      
      var obj = {1: ..., 3: ..., 4: ..., 7: ...};
      var keys= Object.keys(obj); // ["1", "3", "4", "7"]
      var ids = keys.map(parseFloat); // [1, 3, 4, 7]
      
      var arr = ["1", 5, "7", 11];
      var ints= arr.map(parseFloat); // [1, 5, 7, 11]
      ints[1] === "5" // false
      ints[1] === 5   // true
      ints[2] === "7" // false
      ints[2] === 7   // true
      
    2. It's shorter.

    3. It's a tiny bit quickier and takes advantage of cache, when parseInt-approach - doesn't:

        // execution time measure function
        // keep it simple, yeah?
      > var f = (function (arr, c, n, m) {
            var i,t,m,s=n();
            for(i=0;i++<c;)t=arr.map(m);
            return n()-s
        }).bind(null, "2,4,6,8,0,9,7,5,3,1".split(','), 1000000, Date.now);
      
      > f(Number) // first launch, just warming-up cache
      > 3971 // nice =)
      
      > f(Number)
      > 3964 // still the same
      
      > f(function(e){return+e})
      > 5132 // yup, just little bit slower
      
      > f(function(e){return+e})
      > 5112 // second run... and ok.
      
      > f(parseFloat)
      > 3727 // little bit quicker than .map(Number)
      
      > f(parseFloat)
      > 3737 // all ok
      
      > f(function(e){return parseInt(e,10)})
      > 21852 // awww, how adorable...
      
      > f(function(e){return parseInt(e)})
      > 22928 // maybe, without '10'?.. nope.
      
      > f(function(e){return parseInt(e)})
      > 22769 // second run... and nothing changes.
      
      > f(Number)
      > 3873 // and again
      > f(parseFloat)
      > 3583 // and again
      > f(function(e){return+e})
      > 4967 // and again
      
      > f(function(e){return parseInt(e,10)})
      > 21649 // dammit 'parseInt'! >_<
      

    Notice: In Firefox parseInt works about 4 times faster, but still slower than others. In total: +e < Number < parseFloat < parseInt

    0 讨论(0)
  • 2020-12-07 15:22

    ECMAScript5 provides a map method for Arrays, applying a function to all elements of an array. Here is an example:

    var a = ['1','2','3']
    var result = a.map(function (x) { 
      return parseInt(x, 10); 
    });
    
    console.log(result);

    For more information, check https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map

    0 讨论(0)
  • 2020-12-07 15:23

    If you want to convert an Array of digits to a single number just use:

    Number(arrayOfDigits.join(''));
    

    Example

    const arrayOfDigits = [1,2,3,4,5];
    
    const singleNumber = Number(arrayOfDigits.join(''));
    
    console.log(singleNumber); //12345
    
    0 讨论(0)
提交回复
热议问题