回答編集履歴

2

Array.prototype.reduce

2019/09/01 02:24

投稿

think49
think49

スコア18166

test CHANGED
@@ -1,3 +1,7 @@
1
+ ### new Map で管理する
2
+
3
+
4
+
1
5
  同じカテゴリ群を持つデータをグループ化すると、管理性が良くなると思います。
2
6
 
3
7
  元のデータ構造を維持する理由がなければ、`new Map` のまま運用します。
@@ -92,4 +96,74 @@
92
96
 
93
97
 
94
98
 
95
- Re: tetsu777 さん
99
+ ### Array.prototype.reduce
100
+
101
+
102
+
103
+ データ構造を変えず、検索の為の中間オブジェクトをその都度生成するならば、`Array.prototype.reduce` が適していると思われます。
104
+
105
+ (前節のコードもそうですが、カテゴリ1~カテゴリ3は `String` 型であれば、使用できない文字はありません)
106
+
107
+
108
+
109
+ ```JavaScript
110
+
111
+ const array = [
112
+
113
+ ["a","a","a",1,"2019/01/01"],
114
+
115
+ ["a","b","a",1,"2019/01/01"],
116
+
117
+ ["b","a","a",1,"2019/01/01"],
118
+
119
+ ["a","a","c",1,"2019/01/01"],
120
+
121
+ ["c","a","c",1,"2019/01/01"],
122
+
123
+ ["b","b","b",1,"2019/01/01"],
124
+
125
+ ["b","b","a",1,"2019/01/01"],
126
+
127
+ ["c","a","c",1,"2019/07/01"],
128
+
129
+ ["b","a","b",1,"2019/01/01"],
130
+
131
+ ["a","a","a",1,"2019/02/02"]
132
+
133
+ ];
134
+
135
+
136
+
137
+ const lastUpdate = array.reduce((entries, current) => {
138
+
139
+ const keys = entries.keys,
140
+
141
+ values = entries.values,
142
+
143
+ currentKey = JSON.stringify([current[0],current[1],current[2]]),
144
+
145
+ index = keys.indexOf(currentKey);
146
+
147
+
148
+
149
+ if (index === -1) {
150
+
151
+ keys.push(currentKey), values.push(current);
152
+
153
+ } else if (values[index][4] <= current[4]) {
154
+
155
+ keys[index] = currentKey, values[index] = current;
156
+
157
+ }
158
+
159
+
160
+
161
+ return entries;
162
+
163
+ }, {keys: [], values: []}).values;
164
+
165
+
166
+
167
+ console.log(JSON.stringify(lastUpdate)); // [["a","a","a",1,"2019/02/02"],["a","b","a",1,"2019/01/01"],["b","a","a",1,"2019/01/01"],["a","a","c",1,"2019/01/01"],["c","a","c",1,"2019/07/01"],["b","b","b",1,"2019/01/01"],["b","b","a",1,"2019/01/01"],["b","a","b",1,"2019/01/01"]]
168
+
169
+ ```

1

typo修正

2019/09/01 02:24

投稿

think49
think49

スコア18166

test CHANGED
@@ -1,4 +1,4 @@
1
- 同じカテゴリ群を持つデータをグループ化すると、管理性が良くなるといます。
1
+ 同じカテゴリ群を持つデータをグループ化すると、管理性が良くなるといます。
2
2
 
3
3
  元のデータ構造を維持する理由がなければ、`new Map` のまま運用します。
4
4