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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
---
title: Array.prototype.flat()
slug: Web/JavaScript/Reference/Global_Objects/Array/flat
tags:
- Array
- JavaScript
- Prototype
- Referenza
- flat
- metodo
translation_of: Web/JavaScript/Reference/Global_Objects/Array/flat
---
<div></div>
<div>{{JSRef}}</div>
<p><code><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">Il metodo </span></font><strong>flat()</strong></code> crea un nuovo array con tutti gli elementi dei propri sotto-array concatenati ricorsivamente al suo interno fino alla profondità specificata.</p>
<p class="hidden">\{{EmbedInteractiveExample("pages/js/array-flatten.html")}}</p>
<p class="hidden">Il codice sorgente per questo esempio interattivo è mantenuto in una repository GitHub. Se vuoi contribuire al progetto degli esempi interattivi, clona <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> e inviaci una pull request.</p>
<h2 id="Sintassi">Sintassi</h2>
<pre class="syntaxbox notranslate"><var>var newArray = arr</var>.flat(<em>[profondità]</em>);</pre>
<h3 id="Parametri">Parametri</h3>
<dl>
<dt><font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">profondità</span></font> {{optional_inline}}</dt>
<dd>Il livello di profondità che specifica quanto a fondo la struttura di array annidati deve essere appianata. Default a 1.</dd>
</dl>
<h3 id="Valore_di_ritorno">Valore di ritorno</h3>
<p>Un nuovo array con gli elementi dei sotto-array concatenati al suo interno.</p>
<h2 id="Esempi">Esempi</h2>
<h3 id="Appianare_array_annidati">Appianare array annidati</h3>
<pre class="brush: js notranslate">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="Appianamento_e_slot_vuoti_negli_array">Appianamento e slot vuoti negli array</h3>
<p>Il metodo flat rimuove gli slot vuoti in un array:</p>
<pre class="brush: js notranslate">var arr4 = [1, 2, , 4, 5];
arr4.flat();
// [1, 2, 4, 5]
</pre>
<h2 id="Alternative">Alternative</h2>
<h3 id="reduce_e_concat"><code>reduce</code> e <code>concat</code></h3>
<pre class="brush: js notranslate">var arr1 = [1, 2, [3, 4]];
arr1.flat();
//appianare array di un livello
arr1.reduce((acc, val) => acc.concat(val), []);// [1, 2, 3, 4]
//o
const flatSingle = arr => [].concat(...arr);
</pre>
<pre class="brush: js notranslate">//abilitare appianamento a una certà profondità usando reduce e concat
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>
<pre class="brush: js notranslate">//appianamento profondo non ricorsivo usando uno stack
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
function flatten(input) {
const stack = [...input];
const res = [];
while (stack.length) {
// rimozione valore dallo stack
const next = stack.pop();
if (Array.isArray(next)) {
// reinserimento degli elementi degli array, non modifica l'input originale
stack.push(...next);
} else {
res.push(next);
}
}
//reverse per ripristinare l'ordine originario
return res.reverse();
}
flatten(arr1);// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
</pre>
<pre class="brush: js notranslate">//appianamento profondo ricorsivo
function flatten(array) {
var flattend = [];
!(function flat(array) {
array.forEach(function(el) {
if (Array.isArray(el)) flat(el);
else flattend.push(el);
});
})(array);
return flattend;
}
</pre>
<div class="hidden">
<p>Per favore, non aggiungere polyfill a questo articolo. Per referenze, controllare: <a href="https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500">https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500</a></p>
</div>
<h2 id="Specifiche">Specifiche</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specifica</th>
<th scope="col">Stato</th>
<th scope="col">Commenti</th>
</tr>
<tr>
<td>
<p><a href="https://tc39.github.io/proposal-flatMap/#sec-Array.prototype.flat"><code>Array.prototype.flat</code> proposal</a></p>
</td>
<td>Finished (4)</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Compatibilità_con_i_browser">Compatibilità con i browser</h2>
<div>
<div class="hidden">La tabella di compatibilità in questa pagina è generata da dati strutturati. Per favore controlla <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> e mandaci una pull request se vuoi contribuire.</div>
<p>{{Compat("javascript.builtins.Array.flat")}}</p>
</div>
<h2 id="Vedi_anche">Vedi anche</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>
|