回答編集履歴
3
`Array\.prototype\.filter` のコード追記
answer
CHANGED
@@ -52,4 +52,26 @@
|
|
52
52
|
console.log(sample3([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
53
53
|
```
|
54
54
|
|
55
|
+
### Array.prototype.filter
|
56
|
+
|
57
|
+
`Array.prototype.filter` を「4以上」と「4未満」で2回呼び出す方法もあるのですが、そうする場合は配列を2回走査する事になるのでアルゴリズム上は無駄が生じます。
|
58
|
+
配列を一度だけ回すのが美しいと考えています。
|
59
|
+
一応、filter 処理の中でもう一つの配列を作る手がありますが、処理内容は `Array.prototype.reduce` と同じです。
|
60
|
+
|
61
|
+
```JavaScript
|
62
|
+
function sample3 (inputArray, inputNumber) {
|
63
|
+
var smaller = [];
|
64
|
+
|
65
|
+
return inputArray.filter(function (value) {
|
66
|
+
return value < inputNumber ? (smaller.push(value), false) : true;
|
67
|
+
}).concat(smaller);
|
68
|
+
}
|
69
|
+
console.log(sample3([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
70
|
+
```
|
71
|
+
|
72
|
+
### 更新履歴
|
73
|
+
|
74
|
+
- 2017/01/31 14:27 `Array.prototype.reduce` のコード追記
|
75
|
+
- 2017/02/01 00:05 `Array.prototype.filter` のコード追記
|
76
|
+
|
55
77
|
Re: holism さん
|
2
関数名修正\(sample → sample1\)
answer
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
一応、`Array.prototype.reduce` でも実装できますが…。
|
5
5
|
|
6
6
|
```JavaScript
|
7
|
-
function
|
7
|
+
function sample1 (inputArray, inputNumber) {
|
8
8
|
var matched = [], smaller = [];
|
9
9
|
|
10
10
|
for (var i = 0, len = inputArray.length, value; i < len; ++i) {
|
@@ -15,7 +15,7 @@
|
|
15
15
|
return matched.concat(smaller);
|
16
16
|
}
|
17
17
|
|
18
|
-
console.log(
|
18
|
+
console.log(sample1([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
19
19
|
```
|
20
20
|
|
21
21
|
### Array.prototype.reduce
|
1
Array\.prototype\.reduce のコード追記
answer
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
### for 文
|
2
|
+
|
1
3
|
私としては `for` 文で十分と考えます。
|
2
|
-
`Array.prototype.reduce`
|
4
|
+
一応、`Array.prototype.reduce` でも実装できますが…。
|
3
5
|
|
4
6
|
```JavaScript
|
5
7
|
function sample (inputArray, inputNumber) {
|
@@ -16,4 +18,38 @@
|
|
16
18
|
console.log(sample([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
17
19
|
```
|
18
20
|
|
21
|
+
### Array.prototype.reduce
|
22
|
+
|
23
|
+
一方の配列(`smaller`)をスコープ外に置けば `Array.prototype.reduce` でも実装可能です。
|
24
|
+
|
25
|
+
```JavaScript
|
26
|
+
function sample2 (inputArray, inputNumber) {
|
27
|
+
var matched = [], smaller = [];
|
28
|
+
|
29
|
+
return inputArray.reduce(function (matched, current) {
|
30
|
+
current < inputNumber ? smaller.push(current) : matched.push(current);
|
31
|
+
return matched;
|
32
|
+
}, []).concat(smaller);
|
33
|
+
}
|
34
|
+
|
35
|
+
console.log(sample2([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
36
|
+
```
|
37
|
+
|
38
|
+
クロージャとGCの扱いが微妙なのでスコープを調節するとこのように。
|
39
|
+
|
40
|
+
```JavaScript
|
41
|
+
var sample3 = (function () {
|
42
|
+
function reduceCallbackfn (matched, current) {
|
43
|
+
current < this.inputNumber ? this.smaller.push(current) : matched.push(current);
|
44
|
+
return matched;
|
45
|
+
}
|
46
|
+
|
47
|
+
return function sample3 (inputArray, inputNumber) {
|
48
|
+
var smaller = [];
|
49
|
+
return inputArray.reduce(reduceCallbackfn.bind({smaller: smaller, inputNumber: inputNumber}), []).concat(smaller);
|
50
|
+
}
|
51
|
+
}());
|
52
|
+
console.log(sample3([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
53
|
+
```
|
54
|
+
|
19
55
|
Re: holism さん
|