skip to Main Content

I want to put all numbers from min to max vars as a single number to array.
So in my example I want arr = [1,4,1,5,1,6] but i get [1,4, 1,5, ,1,6] as the result.
The problem is when i put separated values in array.
Would be very greatfull for any kind of help.

  let min = 14,
      max = 16,
      arr = [];

for (i=min; i<=max; i++) {
  arrPsh = i.toString().split("");
  arr.push(arrPsh);
}
for (i=0; i<arr.length; i++) {
  console.log(arr[i]);
}

3

Answers


  1. Use spread syntax to pass each digit as a separate array element:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

      let min = 14,
          max = 16,
          arr = [];
    
    for (i=min; i<=max; i++) {
      arrPsh = i.toString().split("");
      arr.push(...arrPsh);
    }
    for (i=0; i<arr.length; i++) {
      document.write(arr[i] + "<br>");
    }

    Actually you don’t need to split the string. The spread syntax gets the default iterator for a variable, accessed with str[Symbol.iterator](). In our case we have a string and its default iterator iterates characters in the string:

      let min = 14,
          max = 16,
          arr = [];
    
    for (i=min; i<=max; i++) {
      arr.push(...i.toString());
    }
    for (i=0; i<arr.length; i++) {
      document.write(arr[i] + "<br>");
    }

    Using Array::flat() would be definitely slower since it creates an intermediate array and introduces some unneeded magic of converting a wrong result to a proper one:

    Cycles: 10000 / Chrome/116
    --------------------------------------------------------
    Alexander    550/min  1.0x   557   561   576   589   550
    jaabaa      1162/min  2.1x  1212  1162  1192  1866  1204
    --------------------------------------------------------
    https://github.com/silentmantra/benchmark
    
    <script benchmark="10000">
    
        let min = 1, max = 500;
    
    // @benchmark Alexander
    {
    const arr = [];
    for (let i=min; i<=max; i++) {
      arr.push(...i.toString());
    }
    arr;
    }
    
    // @benchmark jabaa
    {
    const arr = [];
    for (i=min; i<=max; i++) {
      arrPsh = i.toString().split("");
      arr.push(arrPsh);
    }
    arr.flat();
    }
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
  2. You can flatten the array:

    let min = 14,
        max = 16,
        arr = [];
    
    for (i=min; i<=max; i++) {
      arrPsh = i.toString().split("");
      arr.push(arrPsh);
    }
    arr = arr.flat();
    for (i=0; i<arr.length; i++) {
      document.write(arr[i] + "<br>");
    }
    Login or Signup to reply.
  3. Just join and split the array in the end:

    let min = 14,
        max = 16,
        arr = [];
    
    for (let i=min; i<=max; i++)
        arr.push(i)
    
    arr = arr.join('').split('')
    
    console.log(arr)

    This will be 10x faster than other solutions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search