How to useJavaScript Array flat() Method in Javascript
The Javascript arr.flat() method was introduced in ES2019. It is used to flatten an array, to reduce the nesting of an array. The flat() method is heavily used in the functional programming paradigm of JavaScript.
Syntax:
arr.flat([depth])
Example: In this example, we are using JavaScript Array flat() Method
let arr = [1, 2, 3, , 4];
let newArr = arr.flat();
console.log(newArr);
Output
[ 1, 2, 3, 4 ]
Remove empty elements from an array in JavaScript
Many times there are empty elements in an array. In this article, we will see the methods to remove empty elements from the array.
Below are the approaches used to Remove empty elements from an array in JavaScript:
Table of Content
- Using array.filter() method
- Using array.reduce() method
- Using for loop
- Using JavaScript Array flat() Method