HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
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
This feature is well established and works across many devices and browser versions. It’s been available across browsers since январь 2020 г..
Метод flat() экземпляров Array возвращает новый массив, в котором все элементы вложенных подмассивов рекурсивно "подняты" на указанный уровень.
flat()
Array
flat() flat(depth)
depth
Указывает, на сколько уровней вложенности уменьшается мерность исходного массива. По умолчанию 1.
Новый массив с объединёнными в него подмассивами.
const arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] const arr2 = [1, 2, [3, 4, [5, 6]]]; arr2.flat(); // [1, 2, 3, 4, [5, 6]] const arr3 = [1, 2, [3, 4, [5, 6]]]; arr3.flat(2); // [1, 2, 3, 4, 5, 6] const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]; arr4.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Метод flat() удаляет пустые слоты в массивах:
const arr5 = [1, 2, , 4, 5]; console.log(arr5.flat()); // [1, 2, 4, 5] const array = [1, , 3, ["a", , "c"]]; console.log(array.flat()); // [ 1, 3, "a", "c" ] const array2 = [1, , 3, ["a", , ["d", , "e"]]]; console.log(array2.flat()); // [ 1, 3, "a", ["d", empty, "e"] ] console.log(array2.flat(2)); // [ 1, 3, "a", "d", "e"]
Enable JavaScript to view this browser compatibility table.
Array.prototype.flatMap()
Array.prototype.map()
Array.prototype.reduce()
Array.prototype.concat()