回答編集履歴
3
`Array\.prototype\.filter` のコード追記
test
CHANGED
@@ -106,4 +106,48 @@
|
|
106
106
|
|
107
107
|
|
108
108
|
|
109
|
+
### Array.prototype.filter
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
`Array.prototype.filter` を「4以上」と「4未満」で2回呼び出す方法もあるのですが、そうする場合は配列を2回走査する事になるのでアルゴリズム上は無駄が生じます。
|
114
|
+
|
115
|
+
配列を一度だけ回すのが美しいと考えています。
|
116
|
+
|
117
|
+
一応、filter 処理の中でもう一つの配列を作る手がありますが、処理内容は `Array.prototype.reduce` と同じです。
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```JavaScript
|
122
|
+
|
123
|
+
function sample3 (inputArray, inputNumber) {
|
124
|
+
|
125
|
+
var smaller = [];
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
return inputArray.filter(function (value) {
|
130
|
+
|
131
|
+
return value < inputNumber ? (smaller.push(value), false) : true;
|
132
|
+
|
133
|
+
}).concat(smaller);
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
console.log(sample3([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
138
|
+
|
139
|
+
```
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
### 更新履歴
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
- 2017/01/31 14:27 `Array.prototype.reduce` のコード追記
|
148
|
+
|
149
|
+
- 2017/02/01 00:05 `Array.prototype.filter` のコード追記
|
150
|
+
|
151
|
+
|
152
|
+
|
109
153
|
Re: holism さん
|
2
関数名修正\(sample → sample1\)
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
```JavaScript
|
12
12
|
|
13
|
-
function sample (inputArray, inputNumber) {
|
13
|
+
function sample1 (inputArray, inputNumber) {
|
14
14
|
|
15
15
|
var matched = [], smaller = [];
|
16
16
|
|
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
|
34
34
|
|
35
|
-
console.log(sample([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
35
|
+
console.log(sample1([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
36
36
|
|
37
37
|
```
|
38
38
|
|
1
Array\.prototype\.reduce のコード追記
test
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
+
### for 文
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
私としては `for` 文で十分と考えます。
|
2
6
|
|
3
|
-
`Array.prototype.reduce`
|
7
|
+
一応、`Array.prototype.reduce` でも実装できますが…。
|
4
8
|
|
5
9
|
|
6
10
|
|
@@ -34,4 +38,72 @@
|
|
34
38
|
|
35
39
|
|
36
40
|
|
41
|
+
### Array.prototype.reduce
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
一方の配列(`smaller`)をスコープ外に置けば `Array.prototype.reduce` でも実装可能です。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```JavaScript
|
50
|
+
|
51
|
+
function sample2 (inputArray, inputNumber) {
|
52
|
+
|
53
|
+
var matched = [], smaller = [];
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
return inputArray.reduce(function (matched, current) {
|
58
|
+
|
59
|
+
current < inputNumber ? smaller.push(current) : matched.push(current);
|
60
|
+
|
61
|
+
return matched;
|
62
|
+
|
63
|
+
}, []).concat(smaller);
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
console.log(sample2([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
クロージャとGCの扱いが微妙なのでスコープを調節するとこのように。
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
```JavaScript
|
80
|
+
|
81
|
+
var sample3 = (function () {
|
82
|
+
|
83
|
+
function reduceCallbackfn (matched, current) {
|
84
|
+
|
85
|
+
current < this.inputNumber ? this.smaller.push(current) : matched.push(current);
|
86
|
+
|
87
|
+
return matched;
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
return function sample3 (inputArray, inputNumber) {
|
94
|
+
|
95
|
+
var smaller = [];
|
96
|
+
|
97
|
+
return inputArray.reduce(reduceCallbackfn.bind({smaller: smaller, inputNumber: inputNumber}), []).concat(smaller);
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}());
|
102
|
+
|
103
|
+
console.log(sample3([1, 2, 3, 4, 5, 6, 7], 4)); // [4, 5, 6, 7, 1, 2, 3]
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
|
108
|
+
|
37
109
|
Re: holism さん
|