What is the use of the flat() method in JavaScript?

The flat() method in JavaScript is used to flatten a nested array into a single level array. It returns a new array that is flattened to the specified depth. The method can be very useful when you have an array that contains arrays within it and you want to flatten it into a single array.

The syntax for the flat() method is as follows:

array.flat([depth]);

where depth is an optional parameter that specifies the depth of the flattening operation. If no depth is specified, it defaults to 1.

Here are some examples of how to use the flat() method in JavaScript:

Example 1: Flattening a one-dimensional array:

const arr = [1, 2, 3, 4, 5];
const flattenedArr = arr.flat();
console.log(flattenedArr); // Output: [1, 2, 3, 4, 5]

In this example, we have a one-dimensional array arr. We call the flat() method on it, which returns the same array since it is already one-dimensional.

Example 2: Flattening a two-dimensional array:

const arr = [[1, 2], [3, 4], [5]];
const flattenedArr = arr.flat();
console.log(flattenedArr); // Output: [1, 2, 3, 4, 5]

In this example, we have a two-dimensional array arr. We call the flat() method on it, which returns a one-dimensional array that is the result of flattening arr.

Example 3: Flattening a multi-dimensional array:

const arr = [1, 2, [3, 4, [5, 6]]];
const flattenedArr = arr.flat(2);
console.log(flattenedArr); // Output: [1, 2, 3, 4, 5, 6]

In this example, we have a multi-dimensional array arr. We call the flat() method on it with a depth of 2, which returns a one-dimensional array that is the result of flattening arr up to a depth of 2.

Example 4: Flattening a deeply nested array:

const arr = [1, [2, [3, [4, [5, [6]]]]]];
const flattenedArr = arr.flat(Infinity);
console.log(flattenedArr); // Output: [1, 2, 3, 4, 5, 6]

In this example, we have a deeply nested array arr. We call the flat() method on it with a depth of Infinity, which returns a one-dimensional array that is the result of flattening arr to an unlimited depth.

Example 5: Flattening an array with empty slots:

const arr = [1, 2, , 4, 5];
const flattenedArr = arr.flat();
console.log(flattenedArr); // Output: [1, 2, 4, 5]

In this example, we have an array arr with an empty slot at index 2. We call the flat() method on it, which returns a one-dimensional array that is the result of flattening arr and removing the empty slot.

Additionally, the flat() method can be combined with other array methods to perform complex operations on nested arrays. For example, you can use the map() method to modify the elements of the flattened array:

const arr = [[1, 2], [3, 4], [5]];
const modifiedArr = arr.flat().map((x) => x * 2);
console.log(modifiedArr); // Output: [2, 4, 6, 8, 10]

In this example, we call the flat() method on arr to flatten it into a one-dimensional array, and then call the map() method to multiply each element of the flattened array by 2.

Another example is using the reduce() method to perform a calculation on the elements of the flattened array:

const arr = [[1, 2], [3, 4], [5]];
const sum = arr.flat().reduce((acc, curr) => acc + curr);
console.log(sum); // Output: 15

In this example, we call the flat() method on arr to flatten it into a one-dimensional array, and then call the reduce() method to calculate the sum of its elements.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: