回答編集履歴
4
テキスト修正
test
CHANGED
@@ -188,7 +188,7 @@
|
|
188
188
|
|
189
189
|
```
|
190
190
|
|
191
|
-
というものになり、 `kind` の同じ要素が隣接しないです。
|
191
|
+
というものになり、 `kind` の同じ要素が必ずしも隣接しないです。
|
192
192
|
|
193
193
|
|
194
194
|
|
3
テキスト修正
test
CHANGED
@@ -119,3 +119,77 @@
|
|
119
119
|
|
120
120
|
|
121
121
|
- **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/oNvpGJR](https://codepen.io/jun68ykt/pen/oNvpGJR)
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
### 追記2
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
上記に挙げた2つのコードのように、いったん`kind` の値をキーとして、同じ `kind` の要素を集めるという処理を経なくても、 `array` の要素のうち、`kind` の同じものが他にもある要素だけを残す処理は、(やや無駄もありますが。)以下のように書けます。
|
130
|
+
|
131
|
+
```javascript
|
132
|
+
|
133
|
+
const duplicatedElements = array.filter(
|
134
|
+
|
135
|
+
(e, i) => array.find((e2, j) => e.kind === e2.kind && i !== j)
|
136
|
+
|
137
|
+
)
|
138
|
+
|
139
|
+
```
|
140
|
+
|
141
|
+
ただし、これだと
|
142
|
+
|
143
|
+
```javascript
|
144
|
+
|
145
|
+
const array = [
|
146
|
+
|
147
|
+
{'id': 1, 'category': 'animal', 'kind': 'dog'},
|
148
|
+
|
149
|
+
{'id': 2, 'category': 'fruit', 'kind': 'apple'},
|
150
|
+
|
151
|
+
{'id': 3, 'category': 'fruit', 'kind': 'orange'},
|
152
|
+
|
153
|
+
{'id': 4, 'category': 'animal', 'kind': 'dog'},
|
154
|
+
|
155
|
+
{'id': 5, 'category': 'animal', 'kind': 'cat'},
|
156
|
+
|
157
|
+
{'id': 6, 'category': 'fruit', 'kind': 'grape'},
|
158
|
+
|
159
|
+
{'id': 7, 'category': 'animal', 'kind': 'cat'},
|
160
|
+
|
161
|
+
{'id': 8, 'category': 'fruit', 'kind': 'melon'},
|
162
|
+
|
163
|
+
{'id': 9, 'category': 'fruit', 'kind': 'grape'}
|
164
|
+
|
165
|
+
]
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
のときに、結果として得られる配列が
|
170
|
+
|
171
|
+
```
|
172
|
+
|
173
|
+
[
|
174
|
+
|
175
|
+
{'id':1,'category':'animal','kind':'dog'},
|
176
|
+
|
177
|
+
{'id':4,'category':'animal','kind':'dog'},
|
178
|
+
|
179
|
+
{'id':5,'category':'animal','kind':'cat'},
|
180
|
+
|
181
|
+
{'id':6,'category':'fruit','kind':'grape'},
|
182
|
+
|
183
|
+
{'id':7,'category':'animal','kind':'cat'},
|
184
|
+
|
185
|
+
{'id':9,'category':'fruit','kind':'grape'}
|
186
|
+
|
187
|
+
]
|
188
|
+
|
189
|
+
```
|
190
|
+
|
191
|
+
というものになり、 `kind` の同じ要素が隣接しないです。
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
- **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/bGbaYVR](https://codepen.io/jun68ykt/pen/bGbaYVR)
|
2
テキスト修正
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
例えば、以下でいかがでしょう?
|
6
6
|
|
7
|
-
|
7
|
+
まず、
|
8
8
|
|
9
9
|
```javascript
|
10
10
|
|
@@ -12,11 +12,17 @@
|
|
12
12
|
|
13
13
|
(map, e) => map.set(e.kind, [ ...(map.get(e.kind) || []), e] ), new Map())
|
14
14
|
|
15
|
+
```
|
15
16
|
|
17
|
+
で、 `kind` の値をキーとし、その値の`kind` を持つ要素の配列を値とするマップを作ります。次に
|
18
|
+
|
19
|
+
```javascript
|
16
20
|
|
17
21
|
const duplicatedElements = [...groupingMap].filter(([kind, array]) => array.length > 1 )
|
18
22
|
|
19
23
|
```
|
24
|
+
|
25
|
+
で、マップのエントリを配列にして、その中で、該当要素の配列の長さが1より大きいものだけを抽出します。
|
20
26
|
|
21
27
|
|
22
28
|
|
@@ -48,7 +54,7 @@
|
|
48
54
|
|
49
55
|
|
50
56
|
|
51
|
-
であったとき(※最後の id: 7 の要素を追加しています。)、 `duplicatedElements` の内容を
|
57
|
+
であったとき(※最後の id: 7 の要素を追加しています。)、 `duplicatedElements` の内容を
|
52
58
|
|
53
59
|
|
54
60
|
|
@@ -68,7 +74,7 @@
|
|
68
74
|
|
69
75
|
```
|
70
76
|
|
71
|
-
|
77
|
+
にて、表示すると以下になります。
|
72
78
|
|
73
79
|
```
|
74
80
|
|
1
テキスト修正
test
CHANGED
@@ -89,3 +89,27 @@
|
|
89
89
|
|
90
90
|
|
91
91
|
- **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/gOYoGda](https://codepen.io/jun68ykt/pen/gOYoGda)
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
### 追記
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
配列やオブジェクトの操作で便利なメソッドを提供している、[Lodash](https://lodash.com/) の [_.groupBy](https://lodash.com/docs/#groupBy) を使うと、上記のコードで `groupingMap` 相当のオブジェクトを作ってくれるので、以下のようにも書けます。
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
```javascript
|
104
|
+
|
105
|
+
const groupingObject = _.groupBy(array, e => e.kind)
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
const duplicatedElements = Object.entries(groupingObject).filter(([kind, ary]) => ary.length > 1)
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
- **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/oNvpGJR](https://codepen.io/jun68ykt/pen/oNvpGJR)
|