Horje
JavaScript Program to Create an Array with a Specific Length and Pre-filled Values

In JavaScript, we can create an array with a specific length and pre-filled values using various approaches. This can be useful when we need to initialize an array with default or placeholder values before populating it with actual data.

Method 1: Using the Array() Constructor and fill() Method

The Array constructor can be used to create an array of a specific length. The fill() method can fill this array with a specified value.

Syntax:

const filledArray = new Array(length).fill(value);

Example: Below is the implementation of the above approach

JavaScript
const length = 5; 
const value = 5;
const filledArray = new Array(length).fill(value);
console.log(filledArray); 

Output
[ 5, 5, 5, 5, 5 ]

Method 2: Using a Loop to Initialize Values

We can use a loop to iterate through the desired length and assign values.

Syntax:

const filledArray = Array.from();
for (let i = 0; i < length; i++) {
filledArray.push(value);
}

Example: Below is the implementation of the above approach

JavaScript
const length = 5;
const filledArray = new Array();
for (let i = 0; i < length; i++) {
    filledArray.push(i + 1);
}
console.log(filledArray);

Output
[ 1, 2, 3, 4, 5 ]

Method 3: Using the map() Method

The map() method can be used to create an array by applying a function to each element.

Syntax:

const filledArray = Array.from({ length }).map(() => value);

Example: Below is the implementation of the above approach

JavaScript
const length = 5; 
const filledArray = 
    Array.from({ length })
        .map(() => 5);
console.log(filledArray); 

Output
[ 5, 5, 5, 5, 5 ]

Using Array.from with a Mapping Function

Using Array.from with a mapping function, you can create an array of a specific length with pre-filled values. The mapping function defines the values to fill the array. This approach offers concise and flexible syntax for generating arrays with custom initial values.

Example: In this example we generates a new array filledArray of length 5, filled with the value 0 using Array.from() and an arrow function. It then logs filledArray to the console.

JavaScript
const length = 5;
const value = 0;
const filledArray = Array.from({ length }, () => value);

console.log(filledArray); 

Output
[ 0, 0, 0, 0, 0 ]

Method 5: Using the Spread Operator

In this approach, we use the spread operator (…) to expand an array and then use the Array.prototype.map method to fill it with a specific value. This method provides a concise and flexible way to create an array with pre-filled values.

Syntax:

const filledArray = [...Array(length)].map(() => value);

Example:

JavaScript
const length = 5;
const value = 0;
const filledArray = [...Array(length)].map(() => value);

console.log(filledArray);

Output
[ 0, 0, 0, 0, 0 ]





Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
JavaScript Program to Find the Most Frequent Word in a String JavaScript Program to Find the Most Frequent Word in a String
How to Change Password in Outlook How to Change Password in Outlook
JavaScript Program for Finding the Majority Element of an Array JavaScript Program for Finding the Majority Element of an Array
JavaScript Program to FindNumber of Flips to make Binary String Alternate JavaScript Program to FindNumber of Flips to make Binary String Alternate
Get the details of an element by Atomic Number using Python Get the details of an element by Atomic Number using Python

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11