1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
---
title: Array.prototype.flat()
slug: Web/JavaScript/Reference/Global_Objects/Array/flat
tags:
- Array
- Experimental
- JavaScript
- Method
- Prototype
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Array/flat
---
<div>{{JSRef}} {{SeeCompatTable}}</div>
<p>Die <code><strong>flat()</strong></code> Methode erstellt rekursiv ein neues Array mit allen Elementen von Unterarrays bis zu einer spezifizierten Tiefe.</p>
<p class="hidden">\{{EmbedInteractiveExample("pages/js/array-flatten.html")}}</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox"><var>var newArray = arr</var>.flat(<var>depth</var>);</pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt><code>depth</code> {{optional_inline}}</dt>
<dd>Das Tiefenlevel, welches angibt, bis zu welcher Tiefe die Arraystruktur abgeflacht werden soll. Der Standardwert ist 1.</dd>
</dl>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Ein neues Array, welches die Elemente der Unterarrays enthält.</p>
<h2 id="Beispiele">Beispiele</h2>
<h3 id="Abflachen_von_geschachtelten_Arrays">Abflachen von geschachtelten Arrays</h3>
<pre class="brush: js">var arr1 = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]
var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]
</pre>
<h3 id="Abflachen_und_Löcher_in_Arrays">Abflachen und Löcher in Arrays</h3>
<p>Die <code>flat</code> Methode entfernt leere Plätze in Arrays:</p>
<pre class="brush: js">var arr4 = [1, 2, , 4, 5];
arr4.flat();
// [1, 2, 4, 5]</pre>
<h2 id="Alternative">Alternative</h2>
<h3 id="reduce_und_concat"><code>reduce</code> und <code>concat</code></h3>
<pre>var arr1 = [1, 2, [3, 4]];
arr1.flat();
// Um ein Array um eine Ebene zu glätten.
arr1.reduce((acc, val) => acc.concat(val), []);
// [1, 2, 3, 4]
// Um mehrere Ebenen zu glätten muss reduce und concat rekursiv eingesetzt werden.
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
function flattenDeep(arr1) {
return arr1.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
}
flattenDeep(arr1);
// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
</pre>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Kommentar</th>
</tr>
<tr>
<td><a href="https://tc39.github.io/proposal-flatMap/#sec-Array.prototype.flat"><code>Array.prototype.flat</code> proposal</a></td>
<td>Candidate (3)</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<div>
<p>{{Compat("javascript.builtins.Array.flat")}}</p>
</div>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li>{{jsxref("Array.prototype.flatMap()")}}</li>
<li>{{jsxref("Array.prototype.map()")}}</li>
<li>{{jsxref("Array.prototype.reduce()")}}</li>
<li>{{jsxref("Array.prototype.concat()")}}</li>
</ul>
|