[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap [Back]  [Original]

Array.prototype.flatMap() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.prototype.flatMap()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2020 ..

flatMap() , . map , flat depth ( ) 1, flatMap , .

In this article

var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
    //    new_array
}[, thisArg])

callback

, :

currentValue

.

index

.

array

.

thisArg

this callback.

, callback "" 1.

Array.prototype.map() callback . flatMap map flat depth 1.

map flatMap

js
let arr1 = [1, 2, 3, 4];

arr1.map((x) => [x * 2]);
// [[2], [4], [6], [8]]

arr1.flatMap((x) => [x * 2]);
// [2, 4, 6, 8]

//    
arr1.flatMap((x) => [[x * 2]]);
// [[2], [4], [6], [8]]

map, flatMap .

.

js
let arr1 = ["it's Sunny in", "", "California"];

arr1.map((x) => x.split(" "));
// [["it's","Sunny","in"],[""],["California"]]

arr1.flatMap((x) => x.split(" "));
// ["it's","Sunny","in", "", "California"]

, .

//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

reduce and concat

js
var arr1 = [1, 2, 3, 4];
arr1.flatMap((x) => [x * 2]);
// is equivalent to
arr1.reduce((acc, x) => acc.concat([x * 2]), []);
// [2, 4, 6, 8]

//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.flatmap


Web Proxy Viewer  |  New URL  |  Original Page