teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

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

2015/12/04 14:07

投稿

think49
think49

スコア18194

answer CHANGED
@@ -7,7 +7,7 @@
7
7
  /**
8
8
  * Array.prototype.indexOf 版
9
9
  */
10
- function sample1 (array) {
10
+ function sample_indexOf (array) {
11
11
  var keys = [],
12
12
  values = [],
13
13
  results = [];
@@ -34,7 +34,7 @@
34
34
  /**
35
35
  * ObjectLiteral 版
36
36
  */
37
- function sample2 (array) {
37
+ function sample_ObjectLiteral (array) {
38
38
  var map = {},
39
39
  results = [];
40
40
 
@@ -60,7 +60,7 @@
60
60
  * Map 版 (ES6)
61
61
  * @url https://github.com/paulmillr/es6-shim
62
62
  */
63
- function sample3 (array) {
63
+ function sample_map (array) {
64
64
  var map = new Map,
65
65
  results = [];
66
66
 
@@ -78,11 +78,60 @@
78
78
  return results;
79
79
  }
80
80
 
81
+ /**
82
+ * Array.prototype.forEach + Array.prototype.find 版 (ES6)
83
+ */
84
+ var sample_forEach = (function (findfn) {
85
+ function eachfn (element) {
86
+ var existingElement = this.find(findfn, element[0]);
87
+
88
+ if (typeof existingElement !== 'undefined') {
89
+ existingElement[1] += element[1];
90
+ } else {
91
+ this.push(element);
92
+ }
93
+ }
94
+
95
+ return function sample_forEach (array) {
96
+ var results = [];
97
+
98
+ array.forEach(eachfn, results);
99
+ return results;
100
+ };
101
+ }(function findfn (element) {
102
+ return element[0] === this;
103
+ }));
104
+
105
+ /**
106
+ * Array.prototype.reduce + + Array.prototype.find 版 (ES6)
107
+ */
108
+ var sample_reduce = (function (findfn) {
109
+ function reducefn (previous, current){
110
+ var existingElement = previous.find(findfn, current[0]);
111
+
112
+ if (typeof existingElement !== 'undefined') {
113
+ existingElement[1] += current[1];
114
+ } else {
115
+ previous.push(current);
116
+ }
117
+
118
+ return previous;
119
+ }
120
+
121
+ return function sample_reduce (array) {
122
+ return array.reduce(reducefn, []);
123
+ }
124
+ }(function findfn (element) {
125
+ return element[0] === this;
126
+ }));
127
+
81
128
  var array = [['a',10], ['b',20], ['c',30], ['a',40], ['a',50]];
82
129
 
130
+ console.log(JSON.stringify(sample_indexOf(array))); // [["a",100],["b",20],["c",30]]
131
+ console.log(JSON.stringify(sample_ObjectLiteral(array))); // [["a",100],["b",20],["c",30]]
83
- console.log(JSON.stringify(sample1(array))); // [["a",100],["b",20],["c",30]]
132
+ console.log(JSON.stringify(sample_map(array))); // [["a",100],["b",20],["c",30]]
133
+ console.log(JSON.stringify(sample_forEach(array))); // [["a",100],["b",20],["c",30]]
84
- console.log(JSON.stringify(sample2(array))); // [["a",100],["b",20],["c",30]]
134
+ console.log(JSON.stringify(sample_reduce(array))); // [["a",100],["b",20],["c",30]]
85
- console.log(JSON.stringify(sample3(array))); // [["a",100],["b",20],["c",30]]
86
135
  ```
87
136
 
88
137
  個人的には ES6 の `Map` で `map` オブジェクトを返す仕様に変更する事がお勧めです。
@@ -93,7 +142,16 @@
93
142
  - [Object.prototype.hasOwnProperty() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
94
143
  - [Object.keys() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)
95
144
  - [Map - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Map)
145
+ - [Array.prototype.forEach() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
146
+ - [Array.prototype.reduce() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)
147
+ - [Array.prototype.find() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
96
148
  - [paulmillr/es6-shim](https://github.com/paulmillr/es6-shim)
97
149
  - [ECMAScript 6 compatibility table](http://kangax.github.io/compat-table/es6/)
98
150
 
151
+ ---
152
+
153
+ **(2015/12/04 23:07追記)**
154
+
155
+ `Array#forEach`, `Array#reduce`, `Array#find` を使ったコードを追記しました。
156
+
99
157
  Re: kado_ さん

1

編集テスト

2015/12/04 14:07

投稿

think49
think49

スコア18194

answer CHANGED
File without changes