How to use Apply() and Map() In Javascript
In the below example, we use the apply() method to create an array of 5 elements that can be filled with the map() method. In the map() method, we pass in a callback that returns 0 to fill all the indexes with zeros.
Example: Array.apply() method is used to create an empty array in which we can use the map() method to store each element as key-value pair.
Javascript
const arr = Array.apply( null , Array(5)).map(() => 0); console.log(arr); |
Output
[ 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