| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | [Back] [Original] |
Get to know MDN better
Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since enero de 2020.
El mtodo flatMap() primero mapea cada elemento usando una funcin de mapeo, luego aplana el resultado en una nueva matriz. Es idntico a un map seguido de un flattende profundidad 1, pero flatMap es a menudo til y la fusin de ambos en un mtodo es ligeramente ms eficiente.
const arr1 = [1, 2, 1];
const result = arr1.flatMap((num) => (num === 2 ? [2, 2] : 1));
console.log(result);
// Expected output: Array [1, 2, 2, 1]
var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
// return element for new_array
}[, thisArg])
callbackFuncin que produce un elemento de la nueva matriz, tomando tres argumentos:
currentValueEl elemento actual que se procesa en la matriz.
indexOpcionalEl ndice del elemento actual que se procesa en la matriz.
arrayOpcionalLa matriz map fue llamada.
thisArgOpcionalValor para usar como this al ejecutar callback.
Una nueva matriz con cada elemento es el resultado de la funcin de devolucin de llamada y se aplana a una profundidad de 1.
Ver Array.prototype.map() para una descripcin detallada de la funcin de devolucin de llamada. El mtodo flatMap es idntico a map seguido de una llamada a flatten de profundidad 1.
map y flatMapvar arr1 = [1, 2, 3, 4];
arr1.map((x) => [x * 2]);
// [[2], [4], [6], [8]]
arr1.flatMap((x) => [x * 2]);
// [2, 4, 6, 8]
// solo un nivel es aplanado
arr1.flatMap((x) => [[x * 2]]);
// [[2], [4], [6], [8]]
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
reduce y concatvar arr1 = [1, 2, 3, 4];
arr1.flatMap((x) => [x * 2]);
// es equivalente a
arr1.reduce((acc, x) => acc.concat([x * 2]), []);
// [2, 4, 6, 8]
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Este polyfill necesita Array.prototype.flat polyfill
if (!Array.prototype.flatMap) {
Array.prototype.flatMap = function () {
return Array.prototype.map.apply(this, arguments).flat(1);
};
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.flatmap |
This page was last modified on 29 may 2026 by MDN contributors.
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()toReversed()Array.prototype.toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[@@iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |