質問編集履歴
1
「考えたこと」を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,4 +21,16 @@
|
|
21
21
|
{ createdAt: '2021-09-28', count: 3},
|
22
22
|
{ createdAt: '2021-09-27', count: 1},
|
23
23
|
]
|
24
|
+
```
|
25
|
+
## 考えたこと
|
26
|
+
`reduce`が使えないか考えました。しかし、カウント数の扱い方がよくわかりません。これでは各文字列に対応するcountが求まりません。
|
27
|
+
この場合`reduce`は適切ではないのでしょうか。
|
28
|
+
```javascript
|
29
|
+
const reducer = (previousValue, currentValue, currentIndex, array) => {
|
30
|
+
let count = 1;
|
31
|
+
if(previousValue == currentValue) {
|
32
|
+
count += 1;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
const newDateList = dateList.reduce(reducer, initialValue);
|
24
36
|
```
|