Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. Array .flat() / flatMap()

Array .flat() / flatMap()

Methods for flattening nested arrays. flat() flattens an array up to a specified depth, and flatMap() performs mapping and flattening in a single step.

Syntax

// Flattens a nested array.
var newArray = array.flat(depth);

// Maps each element and flattens the result by one level.
var newArray = array.flatMap(function(element, index, array) {
	return transformedValue;
});

Methods

MethodDescription
flat(depth)Returns a new array with nested arrays flattened up to the specified depth. If depth is omitted, 1 is used. Specifying Infinity removes all levels of nesting.
flatMap(function)Runs a mapping function on each element and returns a new array flattened by one level. Produces the same result as combining array.map() and array.flat(1).

Sample Code

// Flatten one level of nesting.
var arr1 = [1, [2, 3], [4, 5]];
console.log(arr1.flat()); // Outputs "1,2,3,4,5".

// Flatten two levels of nesting.
var arr2 = [1, [2, [3, [4]]]];
console.log(arr2.flat(2)); // Outputs "1,2,3,[4]". Only two levels deep are flattened.

// Use Infinity to remove all levels of nesting.
console.log(arr2.flat(Infinity)); // Outputs "1,2,3,4".

// Use flatMap to produce multiple elements from a single element.
var sentences = ["Hello World", "Foo Bar"];
var words = sentences.flatMap(function(s) {
	return s.split(" ");
});
console.log(words); // Outputs "Hello,World,Foo,Bar".

// Use flatMap to filter and transform at the same time.
var numbers = [1, 2, 3, 4, 5];
var doubled = numbers.flatMap(function(n) {
	return n % 2 === 0 ? [n * 2] : [];
});
console.log(doubled); // Outputs "4,8". Only even numbers are doubled.

Overview

array.flat() is a method added in ES2019 that expands nested arrays (arrays containing other arrays) up to a specified depth into a single flat array. The original array is not modified. The default depth when the argument is omitted is 1, so only one level is flattened.

array.flatMap() combines array.map() and array.flat(1) into a single method. When the result of transforming each element is an array, it is automatically flattened by one level. This makes it easy to generate multiple elements from a single element, or to exclude elements by returning an empty array.

array.flat() also removes empty slots. For example, [1, , 3].flat() returns [1, 3], removing any holes in the array. To flatten all levels of nesting, use array.flat(Infinity).

Browser Compatibility

Chrome Chrome
74+
68 or earlier ×
Firefox Firefox
67+
61 or earlier ×
Safari Safari
18+
11 or earlier ×
Edge Edge
84+
78 or earlier ×
IE IE
11 ×
Not supported in any version
Opera Opera
61+
55 or earlier ×
iOS Safari iOS Safari
18+
11 or earlier ×
Android Browser Android Browser
74+
68 or earlier ×
Chrome Android Chrome Android
74+
68 or earlier ×
Firefox Android Firefox Android
79+
61 or earlier ×

If you find any errors or copyright issues, please .