How to create an array containing 1 to N in Node.js

3 Answers

0 votes
const N = 5
  
let arr = new Array(N); 
  
for (let i = 0; i < arr.length; i++) {
    arr[i] = i;
}
  
console.log(arr);
    
    
    
      
      
/*
run:
      
[ 0, 1, 2, 3, 4 ]
      
*/

 



answered Jul 7, 2022 by avibootz
0 votes
const N = 5
  
const arr = Array.from(
    {length: N},
    (_, index) => index + 1
);
  
console.log(arr);
    
    
      
      
/*
run:
      
[ 1, 2, 3, 4, 5 ]

*/

 



answered Jul 7, 2022 by avibootz
0 votes
const N = 5
  
let arr = Array.apply(null, {
    length: N
}).map(Number.call, Number);
  
console.log(arr);
    
    
      
      
/*
run:
      
[ 0, 1, 2, 3, 4 ]
      
*/

 



answered Jul 7, 2022 by avibootz
...