aboutsummaryrefslogtreecommitdiff
path: root/files/pl/web/javascript/reference/global_objects/array/reduce/index.html
blob: f98d5375c34603c71c0bdfbdd32ada80ee62e501 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
---
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/Referencje/Obiekty/Array/Reduce
---
<div>{{JSRef("Global_Objects", "Array")}}</div>

<h2 id="Summary" name="Summary">Podsumowanie</h2>

<p><code><font face="Open Sans, Arial, sans-serif">Metoda </font><strong>reduce()</strong></code> wywołuje funkcję względem wartości przyrostowej z każdego wywołania i kolejnego elementu tablicy (od lewej do prawej) w celu sprowadzenia tej tablicy do pojedynczej wartości.</p>

<h2 id="Syntax" name="Syntax">Składnia</h2>

<pre class="syntaxbox"><code><var>arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])</var></code></pre>

<h3 id="Parametry">Parametry</h3>

<dl>
 <dt><code>callback</code></dt>
 <dd>Funkcja wykonywana na każdej wartości w tablicy, przyjmuje cztery argumenty:
 <dl>
  <dt><code>previousValue</code></dt>
  <dt></dt>
  <dd>Wartość zwróconą w ostatnim wywołaniu funkcji <code>callback</code>, lub <code>initialValue</code>, jeśli ta została dostarczona. (Patrz niżej.)</dd>
  <dt><code>currentValue</code></dt>
  <dd>Obecnie przetwarzany element w tablicy.</dd>
  <dt><code>index</code></dt>
  <dd>Indeks w tablicy obecnie przetwarzanego elementu.</dd>
  <dt><code>array</code></dt>
  <dd>Tablica, na której została wykonana funkcja <code>reduce</code> .</dd>
 </dl>
 </dd>
 <dt><code>initialValue</code></dt>
 <dd>Opcjonalne. Obiekt który będzie użyty jako pierwszy argument pierwszego wywołania funkcji <code>callback</code>.</dd>
</dl>

<h2 id="Description" name="Description">Opis</h2>

<p><code>reduce</code> wykonuje funkcję <code>callback</code> raz dla każdego elementu występującego w tablicy, wyłączając dziury. Funkcja <code>callback</code> przyjmuje cztery argumenty: wartość początkową (lub wartość poprzedniego wywołania <code>callback)</code>, wartość obecnego elementu, jego indeks, oraz tablicę na której zachodzi proces iteracji.</p>

<p>Przy pierwszym wywołaniu funkcji <code>callback, previousValue</code> oraz <code>currentValue</code> mogą przyjąć jedną z dwóch wartości. Jeżeli <code>initialValue</code> było dostarczone w wywołaniu, wtedy <code>previousValue</code> przyjmie wartość podaną jako <code>initialValue</code>, natomiast <code>currentValue</code> przyjmie wartość pierwszego elementu tablicy. Jeśli <code>initialValue</code> nie było podane, wtedy <code>previousValue</code> będzie miało wartość pierwszego elementu tablicy, natomiast <code>currentValue</code> będzie równe elementowi drugiemu.</p>

<p>Jeżeli tablica jest pusta oraz <code>initialValue</code> nie zostało dostarczone, będzie rzucony błąd {{jsxref("Global_Objects/TypeError", "TypeError")}}. Jeśli natomiast tablica ma jeden tylko element (bez względu na jego pozycję) i <code>initialValue</code> nie zostało podane, lub dostarczono <code>initialValue</code>, ale tablica jest pusta, wtedy ta jedyna wartość zostanie zwrócona, bez wywoływania funkcji <code>callback</code>.</p>

<p>Przyjmijmy, że wystąpiło następujące wywolanie funkcji <code>reduce</code>:</p>

<pre class="brush: js">[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array) {
  return previousValue + currentValue;
});
</pre>

<p>Funkcja callback będzie wywołana cztery razy, z argumentami i wartościami zwrotnymi przy każdym wołaniu jak następuje:</p>

<table style="width: 100%;">
 <thead>
  <tr>
   <th scope="col"></th>
   <th scope="col"><code>previousValue</code></th>
   <th scope="col"><code>currentValue</code></th>
   <th scope="col"><code>index</code></th>
   <th scope="col"><code>array</code></th>
   <th scope="col">wartość zwracana</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <th scope="row">pierwsze wywołanie</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">drugie wywołanie</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">trzecie wywołanie</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">czwarte wywołanie</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>Wartość zwrócona ostatecznie przez <code>reduce</code> będzie tą z ostatniego wywołania funcji callback (<code>10</code>).</p>

<p>Natomiast, jeśli dostarczylibyśmy wartość początkową jako drugi argument funkcji przekazanej do <code>reduce</code>, wynik wyglądałby jak poniżej:</p>

<pre class="brush: js">[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array) {
  return previousValue + currentValue;
}, 10);
</pre>

<table style="width: 100%;">
 <thead>
  <tr>
   <th scope="col"></th>
   <th scope="col"><code>previousValue</code></th>
   <th scope="col"><code>currentValue</code></th>
   <th scope="col"><code>index</code></th>
   <th scope="col"><code>array</code></th>
   <th scope="col">wartość zwracana</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <th scope="row">pierwsze wywołanie</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">drugie wywołanie</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">trzecie wywołanie</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">czwarte wywołanie</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">piąte wywołanie</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>Tym razem wartość zwrócona przez <code>reduce</code> będzie wynosiła <code>20</code>.</p>

<h2 id="Examples" name="Examples">Przykłady</h2>

<h3 id="Example_Sum_up_all_values_within_an_array" name="Example:_Sum_up_all_values_within_an_array">Przykład: Zsumowanie wszystkich wartości w tablicy.</h3>

<pre class="brush: js">var total = [0, 1, 2, 3].reduce(function(a, b) {
  return a + b;
});
// total == 6
</pre>

<h3 id="Example_Flatten_an_array_of_arrays" name="Example:_Flatten_an_array_of_arrays">Przykład: Spłaszczenie tablicy tablic</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" name="Polyfill">Polyfill</h2>

<p><code>Array.prototype.reduce</code> zostało dodane do standardu ECMA-262 w edycji piątej i jako takie może nie występować we wszystkich implementacji standardu. Można obejśc ten brak poprzez wstawienie poniższego kodu na początku skryptu, co pozwala na użycie <code>reduce</code> z implementacjami, które nie wspierają tej funkcji.</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="Specifications" name="Specifications">Specyfikacja</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specyfikacja</th>
   <th scope="col">Stan</th>
   <th scope="col">Komentarz</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.4.4.21', 'Array.prototype.reduce')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Definicja początkowa. Wprowadzon w 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="Browser_compatibility" name="Browser_compatibility">Wspierane przeglądarki</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</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>Feature</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>Basic support</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="See_also" name="See_also">Zobacz też</h2>

<ul>
 <li>{{jsxref("Array.prototype.reduceRight()")}}</li>
</ul>