回答編集履歴
1
コメントを受けてAnswer書き直しました。
answer
CHANGED
@@ -1,19 +1,48 @@
|
|
1
|
-
|
1
|
+
Answer書き直しました。
|
2
2
|
|
3
3
|
```javascript
|
4
|
+
const _ = require('lodash')
|
5
|
+
|
4
|
-
function
|
6
|
+
function filterBydesc (value) {
|
5
7
|
return value == ''
|
6
8
|
? _.constant(true)
|
7
9
|
: function (item) {
|
8
10
|
var ranges = {
|
9
|
-
1: { desc: '
|
11
|
+
1: { desc: 'いう' },
|
12
|
+
2: { desc: 'うえ' },
|
13
|
+
3: { desc: 'かい' },
|
14
|
+
4: { desc: '^あい' } // <---- 条件追加しました。
|
10
15
|
}
|
11
16
|
var range = ranges[value]
|
12
17
|
return _.some(range, function (descword) {
|
13
18
|
return _.some(item.desc, function (word) {
|
14
|
-
return word.
|
19
|
+
return !!word.match(descword) // <---- ここを書き換えました。
|
15
20
|
})
|
16
21
|
})
|
17
22
|
}
|
18
23
|
}
|
24
|
+
|
25
|
+
/* 動作確認用 */
|
26
|
+
console.log(filterBydesc('1')({ desc: { word: 'あいうえお' } })) // true
|
27
|
+
console.log(filterBydesc('2')({ desc: { word: 'あいうえお' } })) // true
|
28
|
+
console.log(filterBydesc('3')({ desc: { word: 'あいうえお' } })) // false
|
29
|
+
|
30
|
+
/* 本題 */
|
31
|
+
console.log(filterBydesc('4')({ desc: { word: 'あいうえお' } })) // true
|
32
|
+
console.log(filterBydesc('4')({ desc: { word: '___あいうえお' } })) // false
|
33
|
+
```
|
34
|
+
|
35
|
+
`_.includes('foobar', 'foo')`は単に true か false を返すので、このケースでは lodash にこだわる必要なさそうでした。
|
36
|
+
`'foobar'.match('^foo')`とすると正規表現込みのマッチングが行われるので、これを使うと良いと思います。
|
37
|
+
|
38
|
+
```javascript
|
39
|
+
'foobar'.match('^foo') // [ 'foo', index: 0, input: 'foobar' ]
|
40
|
+
'___foobar'.match('^foo') // null
|
41
|
+
```
|
42
|
+
|
43
|
+
このままだと返す値が若干怪しいので、`!!`を適用して true か false にしてから返すと良いと思いました。
|
44
|
+
|
45
|
+
```javascript
|
46
|
+
!!'foobar'.match('^foo') // true
|
47
|
+
!!'___foobar'.match('^foo') // false
|
19
48
|
```
|