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

回答編集履歴

5

修正後コード対応

2018/02/06 11:10

投稿

miyabi-sun
miyabi-sun

スコア21465

answer CHANGED
@@ -51,4 +51,72 @@
51
51
  ];
52
52
  console.log(make(users, ids));
53
53
  // こちらもいい感じの結果に
54
+ ```
55
+
56
+ ---
57
+
58
+ 【追記】 グループ分けしたオブジェクトへの対応版
59
+
60
+ 条件変わってますので再度考え直してみます。
61
+ まぁ、オブジェクトはtoPairsを使って配列にしてしまえば消化試合です。
62
+
63
+ キーに引っかかるグループが存在しなかった場合を考慮して、
64
+ 存在しないグループは一度filterで消し飛ばすというガードも組み込みました。
65
+
66
+ ```JavaScript
67
+ const list = {
68
+ 'sports': [
69
+ {identify: 'soccer', name: 'サッカー'},
70
+ {identify: 'baseball', name: '野球'}
71
+ ],
72
+ 'nature': [
73
+ {identify: 'animal', name: '動物'},
74
+ {identify: 'flower', name: '花'}
75
+ ]
76
+ };
77
+ const groups = [
78
+ {identify:'sports', id: 1},
79
+ {identify:'nature', id: 2},
80
+ ];
81
+
82
+ const make = (tmpList, groups) =>
83
+ _(tmpList)
84
+ .toPairs()
85
+ .filter(([gname]) => _.find(groups, group => group.identify === gname))
86
+ .map(([gname, items]) => {
87
+ const group = _.find(groups, group => group.identify === gname);
88
+ return _.map(items, it => ({...it, group_id: group.id}));
89
+ })
90
+ .flatten()
91
+ .value()
92
+
93
+ result = make(list, groups)
94
+ ```
95
+
96
+ [https://codepen.io/travist/full/jrBjBz/](https://codepen.io/travist/full/jrBjBz/)にコピペすればちゃんと表示されることがわかります。
97
+ 結果:
98
+
99
+ ```JSON
100
+ [
101
+ {
102
+ "identify": "soccer",
103
+ "name": "サッカー",
104
+ "group_id": 1
105
+ },
106
+ {
107
+ "identify": "baseball",
108
+ "name": "野球",
109
+ "group_id": 1
110
+ },
111
+ {
112
+ "identify": "animal",
113
+ "name": "動物",
114
+ "group_id": 2
115
+ },
116
+ {
117
+ "identify": "flower",
118
+ "name": "花",
119
+ "group_id": 2
120
+ }
121
+ ]
54
122
  ```

4

syntaxエラーを修正

2018/02/06 11:10

投稿

miyabi-sun
miyabi-sun

スコア21465

answer CHANGED
@@ -7,10 +7,22 @@
7
7
  const make = (tmpList, groups) =>
8
8
  _(tmpList)
9
9
  .map(tmp =>
10
- _.map(groups, group => {...tmp, id: group.id})
10
+ _.map(groups, group => ({...tmp, id: group.id}))
11
11
  )
12
12
  .flatten()
13
13
  .values()
14
+
15
+ const users = [
16
+ { user: 'barney', age: 36, active: true },
17
+ { user: 'fred', age: 40, active: false },
18
+ { user: 'travis', age: 37, active: true}
19
+ ];
20
+ const ids = [
21
+ { id: 123 },
22
+ { id: 234 }
23
+ ];
24
+ console.log(make(users, ids));
25
+ // いい感じにでました
14
26
  ```
15
27
 
16
28
  ---
@@ -22,9 +34,21 @@
22
34
  ```JavaScript
23
35
  const make = (tmpList, groups) =>
24
36
  tmpList
25
- .map(tmp => groups.map(group => {...tmp, id: group.id}))
37
+ .map(tmp => groups.map(group => ({...tmp, id: group.id})))
26
38
  .reduce((arr, items) => {
27
39
  items.forEach(it => { arr.push(it) });
28
40
  return arr;
29
41
  }, [])
42
+
43
+ const users = [
44
+ { user: 'barney', age: 36, active: true },
45
+ { user: 'fred', age: 40, active: false },
46
+ { user: 'travis', age: 37, active: true}
47
+ ];
48
+ const ids = [
49
+ { id: 123 },
50
+ { id: 234 }
51
+ ];
52
+ console.log(make(users, ids));
53
+ // こちらもいい感じの結果に
30
54
  ```

3

tmp使ってなかった

2018/02/06 09:42

投稿

miyabi-sun
miyabi-sun

スコア21465

answer CHANGED
@@ -7,7 +7,7 @@
7
7
  const make = (tmpList, groups) =>
8
8
  _(tmpList)
9
9
  .map(tmp =>
10
- _.map(groups, group => {...tmpList, id: group.id})
10
+ _.map(groups, group => {...tmp, id: group.id})
11
11
  )
12
12
  .flatten()
13
13
  .values()
@@ -22,7 +22,7 @@
22
22
  ```JavaScript
23
23
  const make = (tmpList, groups) =>
24
24
  tmpList
25
- .map(tmp => groups.map(group => {...tmpList, id: group.id}))
25
+ .map(tmp => groups.map(group => {...tmp, id: group.id}))
26
26
  .reduce((arr, items) => {
27
27
  items.forEach(it => { arr.push(it) });
28
28
  return arr;

2

reduce使うルート間違ってるやん

2018/02/06 09:37

投稿

miyabi-sun
miyabi-sun

スコア21465

answer CHANGED
@@ -23,5 +23,8 @@
23
23
  const make = (tmpList, groups) =>
24
24
  tmpList
25
25
  .map(tmp => groups.map(group => {...tmpList, id: group.id}))
26
+ .reduce((arr, items) => {
26
- .reduce((arr, it) => { arr.push(it); return arr; }, [])
27
+ items.forEach(it => { arr.push(it) });
28
+ return arr;
29
+ }, [])
27
30
  ```

1

おまけ追加

2018/02/06 09:32

投稿

miyabi-sun
miyabi-sun

スコア21465

answer CHANGED
@@ -11,4 +11,17 @@
11
11
  )
12
12
  .flatten()
13
13
  .values()
14
+ ```
15
+
16
+ ---
17
+
18
+ 【おまけ】
19
+
20
+ reduce使うルートならネイティブJSでも出来そうなのでやりました。
21
+
22
+ ```JavaScript
23
+ const make = (tmpList, groups) =>
24
+ tmpList
25
+ .map(tmp => groups.map(group => {...tmpList, id: group.id}))
26
+ .reduce((arr, it) => { arr.push(it); return arr; }, [])
14
27
  ```