回答編集履歴
1
テキスト追加
test
CHANGED
@@ -15,3 +15,35 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
別案として、上記のような「いったんjoinしてからreplace」ではないやり方でやるとすれば、以下のようにreduceを使うとできます。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
```javascript
|
26
|
+
|
27
|
+
const fruits = ["Banana", "Orange", "Apple", "Mango", "Banana", "Banana", "Orange", "Apple"];
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
const energy = fruits.reduce((joinedFruits, fruit, i, ary) => {
|
32
|
+
|
33
|
+
const delimiter = i > 0
|
34
|
+
|
35
|
+
? (ary[i-1] === 'Banana' && fruit === 'Orange' ? '・' : ', ' )
|
36
|
+
|
37
|
+
: '';
|
38
|
+
|
39
|
+
return `${joinedFruits}${delimiter}${fruit}`;
|
40
|
+
|
41
|
+
}, '');
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
console.log(energy); // => "Banana・Orange, Apple, Mango, Banana, Banana・Orange, Apple"
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```
|