Last updated: Mar 1, 2024
Reading timeยท4 min
To create an array containing numbers from 1 to N:
Array.from()
method.Array.from()
method will return a new array containing the specified
numbers.const total = 5; const arr = Array.from( {length: total}, (_, index) => index + 1 ); console.log(arr) // ๐๏ธ [1, 2, 3, 4, 5]
The parameters we passed to the Array.from()
method are:
_
character is to denote that we don't need access to the first parameter of the function, which is the value of the array element.A call to Array.from(3)
returns an array with 3 elements with the value of
undefined
.
We used the index of the array element in the function. Javascript indexes are
zero-based, so we had to add 1
to get the desired result.
If you have to do this often, define a reusable function.
function generateArray(size, start) { return Array.from({length: size}, (_, index) => index + start); } console.log(generateArray(3, 1)); // [ 1, 2, 3 ] console.log(generateArray(5, 1)); // [ 1, 2, 3, 4, 5 ] console.log(generateArray(3, 0)); // [ 0, 1, 2 ] console.log(generateArray(5, 3)); // [ 3, 4, 5, 6, 7 ]
The function takes the desired size of the array and the start value as
arguments and generates an array of values from start
to N.
If you want to create an array from 0
to N, you can also use the Array()
constructor.
const arr = Array.from(Array(8).keys()); // [ // 0, 1, 2, 3, // 4, 5, 6, 7 // ] console.log(arr);
We used the Array()
constructor to create an array of 8 empty elements.
The next step is to use the Array.keys()
method to get an iterable of the keys
(indexes) in the array.
// [ <8 empty items> ] console.log(Array(8)); const arr = Array.from(Array(8).keys()); // [ // 0, 1, 2, 3, // 4, 5, 6, 7 // ] console.log(arr);
The last step is to use the Array.from()
method to convert the iterable to an
array.
An alternative to using the Array.from()
method is to use the spread syntax
(...).
const n = 5; const array = [...Array(n + 1).keys()].slice(1); console.log(array); // ๐๏ธ [ 1, 2, 3, 4, 5 ]
We used the spread syntax (...) to unpack the elements of the iterable into a new array.
We used the Array.keys()
method, so the elements start at 0
.
To get an array starting at 1
, we had to use slice()
to omit the first
element from the result.
Alternatively, you can chain a call to the Array.map()
method.
const arr = [...Array(8).keys()].map(index => index + 1); // [ // 1, 2, 3, 4, // 5, 6, 7, 8 // ] console.log(arr);
The function we passed to the Array.map()
method gets called with each element
of the array.
On each iteration, we increment the index by 1
and return the result.
If you want to start the array at 0
, use the Array.keys()
method directly.
const arr = [...Array(8).keys()]; // [ // 0, 1, 2, 3, // 4, 5, 6, 7 // ] console.log(arr);
for
loopThis is a three-step process:
for
loop to iterate N times, starting at 1
.const arr = []; const total = 5; for (let i = 1; i <= total; i++) { arr.push(i); } console.log(arr); // ๐๏ธ [1, 2, 3, 4, 5]
We used a for
loop to iterate N times, pushing the value of the i
variable
into the array.
lodash
If you use the lodash
library, you can also use the
lodash.range() method to create an
array from 1 to N.
import _ from 'lodash'; console.log(_.range(5)); // ๐๏ธ [ 0, 1, 2, 3, 4 ] console.log(_.range(1, 5)); // ๐๏ธ [ 1, 2, 3, 4 ] console.log(_.range(1, 6, 2)); // ๐๏ธ [ 1, 3, 5 ]
If you need to install lodash
, run the following command.
# ๐๏ธ initialize a package.json file if you don't have one npm init -y npm install lodash
The lodash.range()
method takes 3 arguments:
0
.You can learn more about the related topics by checking out the following tutorials: