回答編集履歴
3
findIndexAll 型
test
CHANGED
@@ -90,7 +90,7 @@
|
|
90
90
|
|
91
91
|
|
92
92
|
|
93
|
-
### findIndex
|
93
|
+
### findIndexAll 型
|
94
94
|
|
95
95
|
|
96
96
|
|
2
3パターンのコードを紹介
test
CHANGED
@@ -1,4 +1,354 @@
|
|
1
|
+
### findIndex + findLastIndex 型
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
配列を前方/後方検索し、`index` が一致したら重複なしと見なします。
|
6
|
+
|
7
|
+
アルゴリズムは単純ですが、重複が全くないと配列全体を要素数だけ検索するのでコストがかかります。
|
8
|
+
|
9
|
+
また、`Array.prototype.findLastIndex` が存在しないので自前定義する必要があります。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```JavaScript
|
14
|
+
|
15
|
+
/**
|
16
|
+
|
17
|
+
* findIndex + findLastIndex 型
|
18
|
+
|
19
|
+
*/
|
20
|
+
|
21
|
+
var removeOverlapB1 = (function () {
|
22
|
+
|
23
|
+
function findLastIndex (array, callbackfn, thisArg) {
|
24
|
+
|
25
|
+
var i = array.length, hasThisArg = arguments.length > 2;
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
while (i--) {
|
30
|
+
|
1
|
-
|
31
|
+
if (hasThisArg) {
|
32
|
+
|
33
|
+
if (callbackfn.call(thisArg, array[i], i, array)) {
|
34
|
+
|
35
|
+
return i;
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
} else if (callbackfn(array[i], i, array)) {
|
40
|
+
|
41
|
+
return i;
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
return -1;
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
function findIndexfn (object) {
|
56
|
+
|
57
|
+
return object.b === this;
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
function filterfn (object, i, array) {
|
64
|
+
|
65
|
+
var b = object.b;
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
return array.findIndex(findIndexfn, b) === findLastIndex(array, findIndexfn, b);
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
return function removeOverlapB (array) {
|
76
|
+
|
77
|
+
return array.filter(filterfn);
|
78
|
+
|
79
|
+
};
|
80
|
+
|
81
|
+
}());
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
var array = [{a:1,b:"h"},{a:1,b:"e"},{a:2,b:"l"},{a:3,b:"l"},{a:1,b:"o"}]
|
86
|
+
|
87
|
+
console.log(JSON.stringify(removeOverlapB1(array))); // [{"a":1,"b":"h"},{"a":1,"b":"e"},{"a":2,"b":"l"},{"a":1,"b":"o"}]
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
### findIndex + findLastIndex 型
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
`Array.prototype.findIndex` を発展させ、配列全体からHITした index 値のリストを返す `findIndexAll` を定義し、返り値となる配列の `length` 値が `0` なら重複なしと見なします。
|
98
|
+
|
99
|
+
一度、重複ありと見なしたインデックスをキャッシュし、再検索しないことで検索コストを抑えます。
|
100
|
+
|
101
|
+
ただし、配列全体が検索対象であることは変わりない為、重複が少ないと検索コストが高くなる問題は依然としてあります。
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```JavaScript
|
106
|
+
|
107
|
+
/**
|
108
|
+
|
109
|
+
* findIndexAll 型
|
110
|
+
|
111
|
+
*/
|
112
|
+
|
113
|
+
var removeOverlapB2 = (function (push) {
|
114
|
+
|
115
|
+
function findIndexAll (array, callbackfn, thisArg) {
|
116
|
+
|
117
|
+
var indexes = [], hasThisArg = arguments.length > 2;
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
for (var i = 0, l = array.length; i < l; ++i) {
|
122
|
+
|
123
|
+
if (hasThisArg) {
|
124
|
+
|
125
|
+
if (callbackfn.call(thisArg, array[i], i, array)) {
|
126
|
+
|
127
|
+
indexes.push(i);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
} else if (callbackfn(array[i], i, array)) {
|
132
|
+
|
133
|
+
indexes.push(i);
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
return indexes;
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
function findIndexAllfn (object) {
|
148
|
+
|
149
|
+
return object.b === this;
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
function filterfn (object, i, array) {
|
156
|
+
|
157
|
+
if (this.indexOf(i) !== -1) {
|
158
|
+
|
159
|
+
return false;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
var indexes = findIndexAll(array, findIndexAllfn, object.b);
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
if (indexes.length === 1) {
|
170
|
+
|
171
|
+
return true;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
push.apply(this, indexes);
|
178
|
+
|
179
|
+
return false;
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
return function removeOverlapB (array) {
|
186
|
+
|
187
|
+
return array.filter(filterfn, []);
|
188
|
+
|
189
|
+
};
|
190
|
+
|
191
|
+
}(Array.prototype.push));
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
var array = [{a:1,b:"h"},{a:1,b:"e"},{a:2,b:"l"},{a:3,b:"l"},{a:1,b:"o"}]
|
196
|
+
|
197
|
+
console.log(JSON.stringify(removeOverlapB2(array))); // [{"a":1,"b":"h"},{"a":1,"b":"e"},{"a":2,"b":"l"},{"a":1,"b":"o"}]
|
198
|
+
|
199
|
+
```
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
### while 文 + 後方検索キャッシュ型
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
`while` 文で前方検索/後方検索し、`index` が同値なら重複なしと見なします。
|
208
|
+
|
209
|
+
検索時に重複値をキャッシュする為、一度重複なしと見なされればキャッシュから重複判定出来ます。
|
210
|
+
|
211
|
+
コードが複雑化していますが、私が考えうる限りでは「検索コストが最も低い(無駄が少ない)コード」です。
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
```JavaScript
|
216
|
+
|
217
|
+
/**
|
218
|
+
|
219
|
+
* while 文 + 後方検索キャッシュ型
|
220
|
+
|
221
|
+
*/
|
222
|
+
|
223
|
+
function removeOverlapB3 (array) {
|
224
|
+
|
225
|
+
var i = -1,
|
226
|
+
|
227
|
+
l = array.length,
|
228
|
+
|
229
|
+
j = l,
|
230
|
+
|
231
|
+
conflicts = [],
|
232
|
+
|
233
|
+
values = [],
|
234
|
+
|
235
|
+
results = [];
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
if (l < 2) {
|
240
|
+
|
241
|
+
return array;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
while (++i < l) {
|
248
|
+
|
249
|
+
var current = array[i],
|
250
|
+
|
251
|
+
b = current.b,
|
252
|
+
|
253
|
+
cindex = values.indexOf(b);
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
if (cindex === -1) {
|
258
|
+
|
259
|
+
values.push(b);
|
260
|
+
|
261
|
+
cindex = conflicts.push(false) - 1;
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
while (i < --j) {
|
266
|
+
|
267
|
+
var target = array[j],
|
268
|
+
|
269
|
+
tb = target.b;
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
if (b === tb) {
|
274
|
+
|
275
|
+
conflicts[cindex] = true;
|
276
|
+
|
277
|
+
break;
|
278
|
+
|
279
|
+
} else {
|
280
|
+
|
281
|
+
var tindex = values.indexOf(tb);
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
if (values.indexOf(tb) === -1) {
|
286
|
+
|
287
|
+
values.push(tb);
|
288
|
+
|
289
|
+
conflicts.push(false);
|
290
|
+
|
291
|
+
} else {
|
292
|
+
|
293
|
+
conflicts[tindex] = true;
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
if (i === j) {
|
304
|
+
|
305
|
+
results.push(current);
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
while (++i < l) {
|
310
|
+
|
311
|
+
var current = array[i],
|
312
|
+
|
313
|
+
b = current.b;
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
if (!conflicts[values.indexOf(b)]) {
|
318
|
+
|
319
|
+
results.push(current);
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
break;
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
} else {
|
332
|
+
|
333
|
+
conflicts[cindex] = true;
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
return results;
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
var array = [{a:1,b:"h"},{a:1,b:"e"},{a:2,b:"l"},{a:3,b:"l"},{a:1,b:"o"}]
|
348
|
+
|
349
|
+
console.log(JSON.stringify(removeOverlapB3(array))); // [{"a":1,"b":"h"},{"a":1,"b":"e"},{"a":2,"b":"l"},{"a":1,"b":"o"}]
|
350
|
+
|
351
|
+
```
|
2
352
|
|
3
353
|
|
4
354
|
|
1
要件を勘違いしていたので再考
test
CHANGED
@@ -1,142 +1,4 @@
|
|
1
|
-
|
1
|
+
要件を勘違いしていたので再考します。
|
2
|
-
|
3
|
-
質問文にある「一応これでできたけど」のコードにそれ程無駄はないと思います。
|
4
|
-
|
5
|
-
細かいことを指摘するならいくつかありますが、パフォーマンスUPが切実な問題でなければ問題といえるほどでもないかと…。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
- `arr.length` を毎回評価するのが無駄
|
10
|
-
|
11
|
-
- `arr[i]["b"]` を毎回評価するのが無駄
|
12
|
-
|
13
|
-
- フラグ変数 `flg` が不要
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
```JavaScript
|
18
|
-
|
19
|
-
'use strict';
|
20
|
-
|
21
|
-
/**
|
22
|
-
|
23
|
-
* for 文
|
24
|
-
|
25
|
-
*/
|
26
|
-
|
27
|
-
function removeOverlapB1 (array) {
|
28
|
-
|
29
|
-
var results = [];
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
for (var i = 0, l = array.length; i < l; ++i) {
|
34
|
-
|
35
|
-
var object = array[i],
|
36
|
-
|
37
|
-
b = object.b;
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
for (var j = 0, m = results.length; j < m; ++j) {
|
42
|
-
|
43
|
-
if (results[j].b === b) {
|
44
|
-
|
45
|
-
break;
|
46
|
-
|
47
|
-
}
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
if (j === m) {
|
54
|
-
|
55
|
-
results.push(object);
|
56
|
-
|
57
|
-
}
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
return results;
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
/**
|
70
|
-
|
71
|
-
* reduce + findeIndex
|
72
|
-
|
73
|
-
*/
|
74
|
-
|
75
|
-
var removeOverlapB2 = (function () {
|
76
|
-
|
77
|
-
function findIndexfn (object) {
|
78
|
-
|
79
|
-
return object.b === this.b;
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
function reducefn (before, current) {
|
86
|
-
|
87
|
-
if (before.findIndex(findIndexfn, current) === -1) {
|
88
|
-
|
89
|
-
before.push(current);
|
90
|
-
|
91
|
-
}
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
return before;
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
return function removeOverlapB (array) {
|
102
|
-
|
103
|
-
return array.reduce(reducefn, []);
|
104
|
-
|
105
|
-
};
|
106
|
-
|
107
|
-
}());
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
var array = [{a:1,b:"h"},{a:1,b:"e"},{a:2,b:"l"},{a:3,b:"l"},{a:1,b:"o"}]
|
112
|
-
|
113
|
-
console.log(JSON.stringify(removeOverlapB1(array))); // [{"a":1,"b":"h"},{"a":1,"b":"e"},{"a":2,"b":"l"},{"a":1,"b":"o"}]
|
114
|
-
|
115
|
-
console.log(JSON.stringify(removeOverlapB2(array))); // [{"a":1,"b":"h"},{"a":1,"b":"e"},{"a":2,"b":"l"},{"a":1,"b":"o"}]
|
116
|
-
|
117
|
-
```
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
`reduce`, `findIndex` で処理を小分けにすると見通しが良くなりますが、「**パフォーマンスが上なのは For 文版**」です。
|
122
|
-
|
123
|
-
ですので、要件によっては leila さんが目指した方向も有りだと私は思います。
|
124
|
-
|
125
|
-
それと、「何を無駄と思うのか」の観点は人それぞれだと思いますので自分の制作ポリシーを見つめなおしてみることも重要だと思います。
|
126
|
-
|
127
|
-
例えば、私がここに挙げたコードで比較するなら
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
- 関数はコストが高いので `reduce`, `findeIndex` を使うのが無駄
|
132
|
-
|
133
|
-
- `for` 文の入れ子は変数が増えて見通しが悪くなるので制作効率が低下して無駄に時間がかかる
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
考え方によっては評価が180度変わります。
|
138
|
-
|
139
|
-
ただ、「可読性」については経験によるところが大きいので苦手意識を持たずにいろんな書き方を習得してみると使える武器が増えて良いと思います。
|
140
2
|
|
141
3
|
|
142
4
|
|