回答編集履歴

2

`Array#forEach`, `Array#reduce`, `Array#find` を使ったコードを追記

2015/12/04 14:07

投稿

think49
think49

スコア18164

test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  */
18
18
 
19
- function sample1 (array) {
19
+ function sample_indexOf (array) {
20
20
 
21
21
  var keys = [],
22
22
 
@@ -70,7 +70,7 @@
70
70
 
71
71
  */
72
72
 
73
- function sample2 (array) {
73
+ function sample_ObjectLiteral (array) {
74
74
 
75
75
  var map = {},
76
76
 
@@ -122,7 +122,7 @@
122
122
 
123
123
  */
124
124
 
125
- function sample3 (array) {
125
+ function sample_map (array) {
126
126
 
127
127
  var map = new Map,
128
128
 
@@ -158,15 +158,113 @@
158
158
 
159
159
 
160
160
 
161
+ /**
162
+
163
+ * Array.prototype.forEach + Array.prototype.find 版 (ES6)
164
+
165
+ */
166
+
167
+ var sample_forEach = (function (findfn) {
168
+
169
+ function eachfn (element) {
170
+
171
+ var existingElement = this.find(findfn, element[0]);
172
+
173
+
174
+
175
+ if (typeof existingElement !== 'undefined') {
176
+
177
+ existingElement[1] += element[1];
178
+
179
+ } else {
180
+
181
+ this.push(element);
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
189
+ return function sample_forEach (array) {
190
+
191
+ var results = [];
192
+
193
+
194
+
195
+ array.forEach(eachfn, results);
196
+
197
+ return results;
198
+
199
+ };
200
+
201
+ }(function findfn (element) {
202
+
203
+ return element[0] === this;
204
+
205
+ }));
206
+
207
+
208
+
209
+ /**
210
+
211
+ * Array.prototype.reduce + + Array.prototype.find 版 (ES6)
212
+
213
+ */
214
+
215
+ var sample_reduce = (function (findfn) {
216
+
217
+ function reducefn (previous, current){
218
+
219
+ var existingElement = previous.find(findfn, current[0]);
220
+
221
+
222
+
223
+ if (typeof existingElement !== 'undefined') {
224
+
225
+ existingElement[1] += current[1];
226
+
227
+ } else {
228
+
229
+ previous.push(current);
230
+
231
+ }
232
+
233
+
234
+
235
+ return previous;
236
+
237
+ }
238
+
239
+
240
+
241
+ return function sample_reduce (array) {
242
+
243
+ return array.reduce(reducefn, []);
244
+
245
+ }
246
+
247
+ }(function findfn (element) {
248
+
249
+ return element[0] === this;
250
+
251
+ }));
252
+
253
+
254
+
161
255
  var array = [['a',10], ['b',20], ['c',30], ['a',40], ['a',50]];
162
256
 
163
257
 
164
258
 
259
+ console.log(JSON.stringify(sample_indexOf(array))); // [["a",100],["b",20],["c",30]]
260
+
261
+ console.log(JSON.stringify(sample_ObjectLiteral(array))); // [["a",100],["b",20],["c",30]]
262
+
165
- console.log(JSON.stringify(sample1(array))); // [["a",100],["b",20],["c",30]]
263
+ console.log(JSON.stringify(sample_map(array))); // [["a",100],["b",20],["c",30]]
264
+
166
-
265
+ console.log(JSON.stringify(sample_forEach(array))); // [["a",100],["b",20],["c",30]]
266
+
167
- console.log(JSON.stringify(sample2(array))); // [["a",100],["b",20],["c",30]]
267
+ console.log(JSON.stringify(sample_reduce(array))); // [["a",100],["b",20],["c",30]]
168
-
169
- console.log(JSON.stringify(sample3(array))); // [["a",100],["b",20],["c",30]]
170
268
 
171
269
  ```
172
270
 
@@ -188,10 +286,28 @@
188
286
 
189
287
  - [Map - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Map)
190
288
 
289
+ - [Array.prototype.forEach() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
290
+
291
+ - [Array.prototype.reduce() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)
292
+
293
+ - [Array.prototype.find() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
294
+
191
295
  - [paulmillr/es6-shim](https://github.com/paulmillr/es6-shim)
192
296
 
193
297
  - [ECMAScript 6 compatibility table](http://kangax.github.io/compat-table/es6/)
194
298
 
195
299
 
196
300
 
301
+ ---
302
+
303
+
304
+
305
+ **(2015/12/04 23:07追記)**
306
+
307
+
308
+
309
+ `Array#forEach`, `Array#reduce`, `Array#find` を使ったコードを追記しました。
310
+
311
+
312
+
197
313
  Re: kado_ さん

1

編集テスト

2015/12/04 14:07

投稿

think49
think49

スコア18164

test CHANGED
File without changes