Create an array filled with given values Using JavaScript
Last Updated :
06 Feb, 2023
In JavaScript, an array is a collection of values that can be stored and accessed in a single variable. Each value in an array is called an element, and elements can be of any data type, such as numbers, strings, or other objects. Arrays are indexed, meaning that each element has a numerical position within the array. JavaScript arrays are dynamic, meaning that their size can be changed as needed. They are also zero-indexed, meaning that the first element is at position 0, the second element is at position 1, and so on.
You can create an array filled with given values using the following ways:
- Array fill method: This method is used to fill the array with a given static value.
- Array from method: This method is used to create a new array instance from a given array.
- Array.apply() method: This method is used to create an array filled with given values by passing actual parameters to the apply() method.
- Array spread Operator: It is used to expand an iterable (e.g. array, string, or object) into its individual elements.
- Array for loop method: It is used to Iterate every Individual element in the array.
Method 1: Using JavaScript Array Fill () function.
Approach:
- Declaring an array Given_values with a set of values using the var keyword.
- Creating a new array array using the Array() constructor and passing in an argument of 2 to create an array with 2 empty slots.
- Using the fill() method on the array to fill the empty slots with the Given_values array.
- Printing new array as output.
Example:
JavaScript
<script>
//declaring array with given values
var Given_values = [1, 2, 3, 4, 5];
console.log(`Given values array ${Given_values}`);
//creating an array filled with given values
var filledArray = Array(2).fill(Given_values);
//printing output array
console.log(`Array filled with given values is [${filledArray}]`);
</script>
Output
Given values array 1,2,3,4,5
Array filled with given values is [1,2,3,4,5,1,2,3,4,5]
Method 2: Using Array. From () method.
Approach: In this approach, we can create a new array instance from a given array using Array.from() method
Example:
JavaScript
<script>
//declaring array with Given values
var Given_values = [1, 2, 3, 4, 5];
console.log('Given values array '+Given_values);
//creating an array filled with Given_values array
var filledArray = Array.from({length: 1}, () => Given_values);
//printing output
console.log('Array filled with given values [ '+filledArray+' ]');
</script>
Output
Given values array 1,2,3,4,5
Array filled with given values is [1,2,3,4,5]
Method 3: Using Array.apply() method.
Array. apply() method is called on the Array object and it is passed two arguments:
Approach:
The first argument is null, which sets the value of this inside the function to null.
The second argument is Array (1), which creates an array of length 1.
Then. map(() => values) assigns the values array to each element of the new array. The map() method creates a new array with the results of calling a provided function on every element in the calling array. In this case, the provided function is an arrow function that returns the values array, so the new array is filled with 1 references to the same values array.
Example:
JavaScript
<script>
//declaring array with given values
var givenValues = [1, 2, 3, 4, 5];
console.log('Given elements:'+givenValues);
//create an array filled with given values
var filledArray = Array.apply(null, Array(1)).map((_,i) =>givenValues);
//printing output array
console.log('Array filled with given values [ '+filledArray+' ]');
</script>
Output:
Given values array 1,2,3,4,5
Array filled with given values is [1,2,3,4,5]
Method 4: Filling array using for loop.
Approach:
- initialize an empty array filledArray which will be used to store the values from the givenValues array.
- Declare given values into givenvalues array.
- Then, use for loop to iterate over the elements in the givenValues array.
- On each iteration, the current element is accessed using the index i and is pushed onto the filledArray using the Array.prototype.push() method.
- Finally, the results in the filledArray being filled with the same values as the givenValues array.
Example:
JavaScript
<script>
//declaring array with given values
var givenValues = [1, 2, 3, 4, 5];
console.log('Given elements:'+givenValues);
//declaring empty array
var filledArray = [];
//filling array using for loop
for (let i = 0; i < givenValues.length; i++)
{
filledArray.push(givenValues[i]);
}
//printing output array
console.log('Array filled with given values [ '+filledArray+' ]');
</script>
Output:
Given values array 1,2,3,4,5
Array filled with given values is [1,2,3,4,5]
Method 5: Filling array using spread syntax.
Approach: we will use the spread operator to copy the elements of the givenValues array into a new array. The spread operator allows you to take an iterable (such as an array) and expand its elements into a new array. By wrapping the givenValues array with the spread operator, it creates a new array with the same elements as the givenValues array.
Example:
JavaScript
<script>
//declaring array with given values
var givenValues = [1, 2, 3, 4, 5];
console.log('Given elements:'+givenValues);
//filling array using spread operator
var filledArray = [...givenValues];
//printing output array
console.log('Array filled with given values [ '+filledArray+' ]');
</script>
Output:
Given values array 1,2,3,4,5
Array filled with given values is [1,2,3,4,5]
Similar Reads
How to Create a Dropdown List with Array Values using JavaScript? To add elements to the select element from the JavaScript array, We can also get selected values in the dropdown list using JavaScript. Here we will populate the dropdown list with an array.Below are the methods to create a dropdown list with array values:Table of ContentUsing Pure JavaScriptUsing j
2 min read
Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J
3 min read
How to fill static values in an array in JavaScript ? In this article, we will see the methods to fill an array with some static values. There are many ways to fill static values in an array in JavaScript such as: Using Array fill() MethodUsing for loopUsing push() methodUsing from() methodUsing spread operatorMethod 1: Array fill() Method We use the a
5 min read
How to get Values from HTML Input Array using JavaScript? This problem can be solved by using input tags having the same "name" attribute value that can group multiple values stored under one name which could later be accessed by using that name. Syntaxlet input = document.getElementsByName('array[]');What is an Input Array in HTML?An "input array" in HTML
2 min read
Implode an array with jQuery/JavaScript Given an array of elements and the task is to implode (join the elements of an array) the array elements. Method 1: Using join() method: The join() method can be used to join an array with a separator and return it as a string. The join() method takes in the optional parameter of a separator. The se
2 min read
What is the most efficient way to create a zero filled array in JavaScript ? Creating zero-filled arrays is a common task in JavaScript, essential for initializing data structures, matrices, and more. This guide explores the most efficient methods to create zero-filled arrays, enhancing performance and code readability. Syntax: arr(length).fill(value);Importance of Zero-Fill
2 min read
Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen
7 min read
How to map array values without using map method in JavaScript ? Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each array element. This can also be implemented using a for loop in JavaScript.Approach 1: For this, we can create two arrays, in which o
3 min read
How to create a string by joining the elements of an array in JavaScript ? Given an array containing array elements and here we will join all the array elements to make a single string. To join the array elements we use arr.join() method. There are two methods by which we can create a string by joining the elements of an array: Table of Content Using arr.join() MethodUsing
2 min read
Split an Array into Chunks in JavaScript Here are different methods to split an array into chunks in JavaScript.1. Using slice() MethodThe array slice() method returns a new array containing the selected elements. This method selects the elements starting from the given start argument and ends at, but excluding the given end argument. Synt
3 min read