質問するログイン新規登録

回答編集履歴

4

テキスト修正

2019/09/06 04:47

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -93,6 +93,6 @@
93
93
  {'id':9,'category':'fruit','kind':'grape'}
94
94
  ]
95
95
  ```
96
- というものになり、 `kind` の同じ要素が隣接しないです。
96
+ というものになり、 `kind` の同じ要素が必ずしも隣接しないです。
97
97
 
98
98
  - **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/bGbaYVR](https://codepen.io/jun68ykt/pen/bGbaYVR)

3

テキスト修正

2019/09/06 04:46

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -58,4 +58,41 @@
58
58
  const duplicatedElements = Object.entries(groupingObject).filter(([kind, ary]) => ary.length > 1)
59
59
  ```
60
60
 
61
- - **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/oNvpGJR](https://codepen.io/jun68ykt/pen/oNvpGJR)
61
+ - **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/oNvpGJR](https://codepen.io/jun68ykt/pen/oNvpGJR)
62
+
63
+ ### 追記2
64
+
65
+ 上記に挙げた2つのコードのように、いったん`kind` の値をキーとして、同じ `kind` の要素を集めるという処理を経なくても、 `array` の要素のうち、`kind` の同じものが他にもある要素だけを残す処理は、(やや無駄もありますが。)以下のように書けます。
66
+ ```javascript
67
+ const duplicatedElements = array.filter(
68
+ (e, i) => array.find((e2, j) => e.kind === e2.kind && i !== j)
69
+ )
70
+ ```
71
+ ただし、これだと
72
+ ```javascript
73
+ const array = [
74
+ {'id': 1, 'category': 'animal', 'kind': 'dog'},
75
+ {'id': 2, 'category': 'fruit', 'kind': 'apple'},
76
+ {'id': 3, 'category': 'fruit', 'kind': 'orange'},
77
+ {'id': 4, 'category': 'animal', 'kind': 'dog'},
78
+ {'id': 5, 'category': 'animal', 'kind': 'cat'},
79
+ {'id': 6, 'category': 'fruit', 'kind': 'grape'},
80
+ {'id': 7, 'category': 'animal', 'kind': 'cat'},
81
+ {'id': 8, 'category': 'fruit', 'kind': 'melon'},
82
+ {'id': 9, 'category': 'fruit', 'kind': 'grape'}
83
+ ]
84
+ ```
85
+ のときに、結果として得られる配列が
86
+ ```
87
+ [
88
+ {'id':1,'category':'animal','kind':'dog'},
89
+ {'id':4,'category':'animal','kind':'dog'},
90
+ {'id':5,'category':'animal','kind':'cat'},
91
+ {'id':6,'category':'fruit','kind':'grape'},
92
+ {'id':7,'category':'animal','kind':'cat'},
93
+ {'id':9,'category':'fruit','kind':'grape'}
94
+ ]
95
+ ```
96
+ というものになり、 `kind` の同じ要素が隣接しないです。
97
+
98
+ - **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/bGbaYVR](https://codepen.io/jun68ykt/pen/bGbaYVR)

2

テキスト修正

2019/09/06 04:34

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -1,13 +1,16 @@
1
1
  こんにちは
2
2
 
3
3
  例えば、以下でいかがでしょう?
4
-
4
+ まず、
5
5
  ```javascript
6
6
  const groupingMap = array.reduce(
7
7
  (map, e) => map.set(e.kind, [ ...(map.get(e.kind) || []), e] ), new Map())
8
-
8
+ ```
9
+ で、 `kind` の値をキーとし、その値の`kind` を持つ要素の配列を値とするマップを作ります。次に
10
+ ```javascript
9
11
  const duplicatedElements = [...groupingMap].filter(([kind, array]) => array.length > 1 )
10
12
  ```
13
+ で、マップのエントリを配列にして、その中で、該当要素の配列の長さが1より大きいものだけを抽出します。
11
14
 
12
15
  上記に対して、元の `array` が以下
13
16
 
@@ -23,7 +26,7 @@
23
26
  ]
24
27
  ```
25
28
 
26
- であったとき(※最後の id: 7 の要素を追加しています。)、 `duplicatedElements` の内容を表示すると以下になります。
29
+ であったとき(※最後の id: 7 の要素を追加しています。)、 `duplicatedElements` の内容を
27
30
 
28
31
  ```javascript
29
32
  duplicatedElements.forEach(([kind, array]) => {
@@ -33,7 +36,7 @@
33
36
  })
34
37
  })
35
38
  ```
36
- 上記より、以下のよう表示されます。
39
+ 表示すると以下になります。
37
40
  ```
38
41
  dog:
39
42
  {'id':1,'category':'animal','kind':'dog'}

1

テキスト修正

2019/09/06 04:13

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -43,4 +43,16 @@
43
43
  {'id':7,'category':'animal','kind':'cat'}
44
44
  ```
45
45
 
46
- - **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/gOYoGda](https://codepen.io/jun68ykt/pen/gOYoGda)
46
+ - **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/gOYoGda](https://codepen.io/jun68ykt/pen/gOYoGda)
47
+
48
+ ### 追記
49
+
50
+ 配列やオブジェクトの操作で便利なメソッドを提供している、[Lodash](https://lodash.com/) の [_.groupBy](https://lodash.com/docs/#groupBy) を使うと、上記のコードで `groupingMap` 相当のオブジェクトを作ってくれるので、以下のようにも書けます。
51
+
52
+ ```javascript
53
+ const groupingObject = _.groupBy(array, e => e.kind)
54
+
55
+ const duplicatedElements = Object.entries(groupingObject).filter(([kind, ary]) => ary.length > 1)
56
+ ```
57
+
58
+ - **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/oNvpGJR](https://codepen.io/jun68ykt/pen/oNvpGJR)