How to use Lodash _.fill() Method In Javascript
Lodash _.fill() method is used to fill a set of values into the array in a given range.
Syntax:
_.fill(array, value, startIndex, endIndex);
Example: This example shows the use of the Lodash _.fill() method.
Javascript
// Method 5: Using Lodash _.fill() Method const arrayLength = 10; // Set the desired length of the array const zeroFilledArray = _.fill(Array(arrayLength), 0); // Display the result console.log(zeroFilledArray); |
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Creating a Zero-Filled Array in JavaScript
A zero-filled array is an array whose value at each index is zero.
Below are the methods used for creating a Zero-Filled Array in JavaScript:
Table of Content
- Array.prototype.fill
- Using Apply() and Map()
- Using Array.from()
- Using for loop
- Using Lodash _.fill() Method