aboutsummaryrefslogtreecommitdiff
path: root/files/ca/web/javascript/reference/global_objects/array/reduce
diff options
context:
space:
mode:
Diffstat (limited to 'files/ca/web/javascript/reference/global_objects/array/reduce')
-rw-r--r--files/ca/web/javascript/reference/global_objects/array/reduce/index.html305
1 files changed, 0 insertions, 305 deletions
diff --git a/files/ca/web/javascript/reference/global_objects/array/reduce/index.html b/files/ca/web/javascript/reference/global_objects/array/reduce/index.html
deleted file mode 100644
index e755078556..0000000000
--- a/files/ca/web/javascript/reference/global_objects/array/reduce/index.html
+++ /dev/null
@@ -1,305 +0,0 @@
----
-title: Array.prototype.reduce()
-slug: Web/JavaScript/Reference/Global_Objects/Array/Reduce
-translation_of: Web/JavaScript/Reference/Global_Objects/Array/Reduce
-original_slug: Web/JavaScript/Referencia/Objectes_globals/Array/Reduce
----
-<div>{{JSRef}}</div>
-
-<p>El mètode <code><strong>reduce()</strong></code> aplica una funció sobre un acumulador i cada valor de l'array (de esquerra a dreta) perr a reduir-lo a un sol valor.</p>
-
-<h2 id="sintaxi">sintaxi</h2>
-
-<pre class="syntaxbox"><code><var>arr</var>.reduce(<var>callback</var>[, valorInicial])</code></pre>
-
-<h3 id="Parameters">Parameters</h3>
-
-<dl>
- <dt><code>callback</code></dt>
- <dd>Funció a executar per a cada valor de l'array. Rep quatre arguments:
- <dl>
- <dt><code>valorPrevi</code></dt>
- <dd>El valor retornat prèviament en l'última invocació de la funció <code>callback</code>, o bé <code>valorInicial</code>, si s'ha proporcionat (vegeu més abaix).</dd>
- <dt><code>valorActual</code></dt>
- <dd>L'element essent processat actualment a l'array.</dd>
- <dt><code>index</code></dt>
- <dd>La posició de l'element essent processat actualment a l'array.</dd>
- <dt><code>array</code></dt>
- <dd>L'array al qual s'ha cridat el mètode <code>reduce</code>.</dd>
- </dl>
- </dd>
- <dt><code>valorInicial</code></dt>
- <dd>Opcional. Valor a utilitzar com a primer argument a la primera crida de la funció <code>callback</code>.</dd>
-</dl>
-
-<h2 id="Descripció">Descripció</h2>
-
-<p><code>reduce</code> executa la funció <code>callback</code> un cop per cada element present a l'array, excloent forats a l'array, i rep quatre arguments:</p>
-
-<ul>
- <li><code>valorPrevi</code></li>
- <li><code>valorActual</code></li>
- <li><code>index</code></li>
- <li><code>array</code></li>
-</ul>
-
-<p>El primer cop que es crida <code>callback</code>, <code>valorAnterior</code> i <code>valorActual</code> reben el valor de la forma descrita a continuació. Si es proporciona <code>valorInicial</code> a la crida de <code>reduce</code>, <code>valorAnterior</code> rebrà el valor de <code>valorInicial</code> i <code>valorActual</code> serà igual al primer valor de l'array. Si no es proporciona <code>valorInicial</code>, <code>valorAnterior</code> serà igual al primer valor de l'array i <code>valorActual</code> serà igual al segon.</p>
-
-<p>Si l'array és buit i no s'ha proporcionat <code>valorInicial</code>, es llençarà {{jsxref("Global_Objects/TypeError", "TypeError")}}. Si l'array només té un element (sense importar la seva posició) i no s'ha proporcionat <code>valorInicial</code>, o si <code>valorInicial</code> s'ha proporcionat però l'array és buit, es retornarà aquest únic valor sense realitzar cap crida a <code>callback</code>.</p>
-
-<p>Suposem que s'ha utilitzar <code>reduce</code> de la forma següent:</p>
-
-<pre class="brush: js">[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array) {
- return previousValue + currentValue;
-});
-</pre>
-
-<p>La funció <code>callback</code> es cridarà quatre cops, on els arguments i els valors a retornar es mostren a continuació:</p>
-
-<table>
- <thead>
- <tr>
- <th scope="col"> </th>
- <th scope="col"><code>valorAnterior</code></th>
- <th scope="col"><code>valorActual</code></th>
- <th scope="col"><code>index</code></th>
- <th scope="col"><code>array</code></th>
- <th scope="col">valor retornat</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <th scope="row">primera crida</th>
- <td><code>0</code></td>
- <td><code>1</code></td>
- <td><code>1</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>1</code></td>
- </tr>
- <tr>
- <th scope="row">segons crida</th>
- <td><code>1</code></td>
- <td><code>2</code></td>
- <td><code>2</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>3</code></td>
- </tr>
- <tr>
- <th scope="row">tercera crida</th>
- <td><code>3</code></td>
- <td><code>3</code></td>
- <td><code>3</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>6</code></td>
- </tr>
- <tr>
- <th scope="row">quarta crida</th>
- <td><code>6</code></td>
- <td><code>4</code></td>
- <td><code>4</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>10</code></td>
- </tr>
- </tbody>
-</table>
-
-<p>El valor retornat per <code>reduce</code> serà el de l'última invocació a <code>callback</code> (<code>10</code>).</p>
-
-<p>Si es proporcionés el valor inicial com a segon argument de <code>reduce</code>, el resultat seria el següent:</p>
-
-<pre class="brush: js">[0, 1, 2, 3, 4].reduce(function(valorAnterior, valorActual, index, array) {
- return valorAnterior + valorActual;
-}, 10);
-</pre>
-
-<table>
- <thead>
- <tr>
- <th scope="col"> </th>
- <th scope="col"><code>valorAnterior</code></th>
- <th scope="col"><code>valorActual</code></th>
- <th scope="col"><code>index</code></th>
- <th scope="col"><code>array</code></th>
- <th scope="col">valor retornat</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <th scope="row">primera crida</th>
- <td><code>10</code></td>
- <td><code>0</code></td>
- <td><code>0</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>10</code></td>
- </tr>
- <tr>
- <th scope="row">segona crida</th>
- <td><code>10</code></td>
- <td><code>1</code></td>
- <td><code>1</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>11</code></td>
- </tr>
- <tr>
- <th scope="row">tercera crida</th>
- <td><code>11</code></td>
- <td><code>2</code></td>
- <td><code>2</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>13</code></td>
- </tr>
- <tr>
- <th scope="row">quarta crida</th>
- <td><code>13</code></td>
- <td><code>3</code></td>
- <td><code>3</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>16</code></td>
- </tr>
- <tr>
- <th scope="row">cinquena crida</th>
- <td><code>16</code></td>
- <td><code>4</code></td>
- <td><code>4</code></td>
- <td><code>[0, 1, 2, 3, 4]</code></td>
- <td><code>20</code></td>
- </tr>
- </tbody>
-</table>
-
-<p>El valor de la crida final (<code>20</code>) és el retornat per la funció <code>reduce</code>.</p>
-
-<h2 id="Exemples">Exemples</h2>
-
-<h3 id="Sumar_tots_els_valors_d'un_array">Sumar tots els valors d'un array</h3>
-
-<pre class="brush: js">var total = [0, 1, 2, 3].reduce(function(a, b) {
- return a + b;
-});
-// total == 6
-</pre>
-
-<h3 id="Aplanar_un_array_d'arrays"><em>Aplanar</em> un array d'arrays</h3>
-
-<pre class="brush: js">var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
- return a.concat(b);
-}, []);
-// flattened is [0, 1, 2, 3, 4, 5]
-</pre>
-
-<h2 id="Polyfill">Polyfill</h2>
-
-<p><code>Array.prototype.reduce</code> va ser afegida a l'standard ECMA-262 a la cinquena edició; degut a això aquesta no estar present a totes les implementacions de l'standard. És possible simular-la en aquests casos mitjançant l'inserció del codi que trobareu a continuació a l'inici dels vostres scripts, tot permetent-vos utilitzar <code>reduce</code> en implementacions que no la suportin de forma nativa.</p>
-
-<pre class="brush: js">// Production steps of ECMA-262, Edition 5, 15.4.4.21
-// Reference: http://es5.github.io/#x15.4.4.21
-if (!Array.prototype.reduce) {
- Array.prototype.reduce = function(callback /*, initialValue*/) {
- 'use strict';
- if (this == null) {
- throw new TypeError('Array.prototype.reduce called on null or undefined');
- }
- if (typeof callback !== 'function') {
- throw new TypeError(callback + ' is not a function');
- }
- var t = Object(this), len = t.length &gt;&gt;&gt; 0, k = 0, value;
- if (arguments.length == 2) {
- value = arguments[1];
- } else {
- while (k &lt; len &amp;&amp; !(k in t)) {
- k++;
- }
- if (k &gt;= len) {
- throw new TypeError('Reduce of empty array with no initial value');
- }
- value = t[k++];
- }
- for (; k &lt; len; k++) {
- if (k in t) {
- value = callback(value, t[k], k, t);
- }
- }
- return value;
- };
-}
-</pre>
-
-<h2 id="Especificacions">Especificacions</h2>
-
-<table class="standard-table">
- <tbody>
- <tr>
- <th scope="col">Especificació</th>
- <th scope="col">Estat</th>
- <th scope="col">Comentaris</th>
- </tr>
- <tr>
- <td>{{SpecName('ES5.1', '#sec-15.4.4.21', 'Array.prototype.reduce')}}</td>
- <td>{{Spec2('ES5.1')}}</td>
- <td>Definició inicial. Implementat a JavaScript 1.8.</td>
- </tr>
- <tr>
- <td>{{SpecName('ES6', '#sec-array.prototype.reduce', 'Array.prototype.reduce')}}</td>
- <td>{{Spec2('ES6')}}</td>
- <td> </td>
- </tr>
- </tbody>
-</table>
-
-<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>
-
-<div>{{CompatibilityTable}}</div>
-
-<div id="compat-desktop">
-<table class="compat-table">
- <tbody>
- <tr>
- <th>Característica</th>
- <th>Chrome</th>
- <th>Firefox (Gecko)</th>
- <th>Internet Explorer</th>
- <th>Opera</th>
- <th>Safari</th>
- </tr>
- <tr>
- <td>Suport bàsic</td>
- <td>{{CompatVersionUnknown}}</td>
- <td>{{CompatGeckoDesktop("1.9")}}</td>
- <td>{{CompatIE("9")}}</td>
- <td>{{CompatOpera("10.5")}}</td>
- <td>{{CompatSafari("4.0")}}</td>
- </tr>
- </tbody>
-</table>
-</div>
-
-<div id="compat-mobile">
-<table class="compat-table">
- <tbody>
- <tr>
- <th>Característica</th>
- <th>Android</th>
- <th>Chrome for Android</th>
- <th>Firefox Mobile (Gecko)</th>
- <th>IE Mobile</th>
- <th>Opera Mobile</th>
- <th>Safari Mobile</th>
- </tr>
- <tr>
- <td>Suport bàsic</td>
- <td>{{CompatVersionUnknown}}</td>
- <td>{{CompatVersionUnknown}}</td>
- <td>{{CompatVersionUnknown}}</td>
- <td>{{CompatVersionUnknown}}</td>
- <td>{{CompatVersionUnknown}}</td>
- <td>{{CompatVersionUnknown}}</td>
- </tr>
- </tbody>
-</table>
-</div>
-
-<h2 id="Vegeu_també">Vegeu també</h2>
-
-<ul>
- <li>{{jsxref("Array.prototype.reduceRight()")}}</li>
-</ul>