• Home
  • Insurance
  • Banking
  • Loans
  • Remitance
  • About us
Facebook Twitter Instagram
  • links
Biz Assurance
Subscribe
  • Home
  • Insurance
  • Banking
  • Loans
  • Remitance
  • About us
Biz Assurance
Home»Tech»How to Create an Array Containing 1…N
Tech

How to Create an Array Containing 1…N

Alicia CormieBy Alicia CormieNo Comments4 Mins Read
Facebook Twitter WhatsApp
Share
Facebook Twitter LinkedIn WhatsApp

Creating an array of “N” numbers manually, such as 1000 or more, can be hectic and time-consuming. So, you can easily create any length of an array in JavaScript without typing manually. To do this, use the JavaScript built-in methods or approaches, like the “for” loop, “Array.from()” method or the “spread operator”.

This tutorial will demonstrate the methods to create an array of “N” numbers.

How to Create an Array Containing 1…N?

For creating an array that contains “n” numbers starting from 1, use the following methods:

  • for loop
  • from() method
  • Spread operator

Method 1: Create an Array Containing 1 … N Using “for” Loop

Use the “for” loop with the “push()” method for creating an array of “N” numbers starting from any number such as “1”. It is the most commonly used method.

Syntax

Use the provided syntax of the “for” loop for creating an array:

for (var i = 1; i <= n; i++) {
 //……
}

Let’s look at an example of creating an array using the “for” loop.

Example

First, create an empty array:

const array = [];

Create a variable “n”, which stores the length of an array. Here, we create an array of numbers 1 to 10:

var n = 10;

Use the for() loop to print “n” numbers by assigning values to an empty array using the “push()” method:

for (var i = 1; i <= n; i++) {
 array.push(i);
}

To begin with a number other than one, simply change the initial value of the variable “i”.

Finally, print the array on the console:

console.log(array);

It can be observed that the array containing numbers 1 to N has been successfully created:

Method 2: Create an Array Containing 1 … N Using “Array.from()” Method

You can also utilize the “Array.from()” method to create an array with 1 to N numbers in it. It outputs an array from any object with a length property.

Syntax

Follow the mentioned syntax for the Array.from() method to create an array containing numbers 1 to N:

Array.from(arrayLike, (element, index) => {
//….
})

Example

First, create a variable “array” that stores the resultant array by calling the “Array.from()” method. There is a prebuilt “map()” method in Array.from() method that iterates the elements and passes the current index and item to the function to create an array instance:

const array = Array.from(
 {length: 10},
 (item, index) => item = index + 1
);

Lastly, print the resultant array returned from the Array.from() method:

console.log(array);

Output

Method 3: Create an Array Containing 1 … N Using “Spread” Operator

Another way to create an array containing 1 to N is the “Spread operator” with the “keys()” and the “map()” methods. It first creates an array of N empty elements, and then the “keys()” method on the Array will return an iterator containing the keys (indices) of the elements in the array. The spread operator is then used to spread the keys into a new array, and the “map()” method is used to add 1 to each key, resulting in an array containing the numbers from 1 to N.

Syntax

For creating an array using the spread operator, use the given syntax:

[…Array(N).keys()].map(i => i + 1)

Here, “Array(N)” will create an array of N empty elements.

Example

Use the spread operator with the Array constructor by passing “N=10” with keys() method:

const array = […Array(10).keys()].map(i => i + 1);

It will print “N” numbers starting from 1 to 10:

We have gathered all the essential information relevant for creating an array containing 1 to N.

Conclusion

To create an array containing 1 to N, use the “for” loop with “push()” method, “Array.from()” method, or the “Spread operator” with the “key()” and the “map()” methods. In the first approach, the push() method is used with a for loop to add elements to an array. The Array.from() method has a pre-built map() method, while the third approach explicitly calls the map() method. In this tutorial, we demonstrated the methods for creating an array of “N” numbers.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleIs There a Way to Rollback my Last Push to Git?
Next Article How to Create Branch and Checkout Using a Single Command

Related Posts

100 Best Applications for Ubuntu 2023

7 Best Ubuntu Screen Recorders for Everyday Use

How to Fix “Not connected – No connections are available” Error in Windows

Add A Comment

Leave A Reply Cancel Reply

Toosii Gave The List Of His Favorite Songs At Rolling Loud, And There’s No Surprise As To Who Made The Cut

Toosii’s First Date Rules And Financial Expectations At Rolling Loud Are Sure To Upset The Manosphere

‘WWE 2K23’ Gets Even Stronger With WarGames And Rivalry Actions

Colin Farrell Called Out ‘SNL’ For Mocking Irish Stereotypes At The Oscars

  • Homepage
  • Sitemap
© 2023 Biz Assurance - Designed by Curtiex Ventures.

Type above and press Enter to search. Press Esc to cancel.