aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/javascript/reference/global_objects/array/flat/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:42:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:42:17 -0500
commitda78a9e329e272dedb2400b79a3bdeebff387d47 (patch)
treee6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/it/web/javascript/reference/global_objects/array/flat/index.html
parent1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff)
downloadtranslated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz
translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2
translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip
initial commit
Diffstat (limited to 'files/it/web/javascript/reference/global_objects/array/flat/index.html')
-rw-r--r--files/it/web/javascript/reference/global_objects/array/flat/index.html159
1 files changed, 159 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/global_objects/array/flat/index.html b/files/it/web/javascript/reference/global_objects/array/flat/index.html
new file mode 100644
index 0000000000..3c5a81ed4b
--- /dev/null
+++ b/files/it/web/javascript/reference/global_objects/array/flat/index.html
@@ -0,0 +1,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) =&gt; acc.concat(val), []);// [1, 2, 3, 4]
+
+//o
+const flatSingle = arr =&gt; [].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) =&gt; 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>