JavaScript flatMap and fill.
Hi friends, in this article I will explain JavaScript flatMap and fill method. These two methods are array methods.
flatMap
The flatMap()
method returns a new array formed by applying a given callback function to each element of the array and then flattening the result by one level. It is identical to a map()
followed by a flat()
of depth 1, but slightly more efficient than calling those two methods separately.
Example
The above example returns flat the 1 depth array and multiplies with 10.
fill
The fill()
method changes all elements in an array to a static value, from a start index (default 0
) to an end index (default array.length
). It returns the modified array.
Example
In the above example fill with static value in the array element. 0
is the static value. 1
is the starting index 3
is the ending index. It returns [ 1, 0, 0, 4 ]
.
Thank you for reading. Have a nice day!
Reference links
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap