回答編集履歴

2

テキスト追加

2020/05/20 00:10

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -91,3 +91,117 @@
91
91
 
92
92
 
93
93
  以上、参考になれば幸いです。
94
+
95
+
96
+
97
+ ### 追記
98
+
99
+
100
+
101
+ 別案です。同じ sensor_group_id ごとのグルーピングを行うために、lodash の [_.groupBy](https://lodash.com/docs/#groupBy) を使います。
102
+
103
+
104
+
105
+ ```javascript
106
+
107
+ const data = {
108
+
109
+ group: [
110
+
111
+ {
112
+
113
+ uid: 1001,
114
+
115
+ list: [
116
+
117
+ { sensor_group_id: 117 },
118
+
119
+ { sensor_group_id: 118 }
120
+
121
+ ]
122
+
123
+ },
124
+
125
+ {
126
+
127
+ uid: 1002,
128
+
129
+ list: [
130
+
131
+  { sensor_group_id: 119 },
132
+
133
+  ]
134
+
135
+ },
136
+
137
+ {
138
+
139
+ uid: 1003,
140
+
141
+ list: [
142
+
143
+  { sensor_group_id: 116 },
144
+
145
+  { sensor_group_id: 117 },
146
+
147
+  ]
148
+
149
+ },
150
+
151
+ ]
152
+
153
+ }
154
+
155
+
156
+
157
+ const pairs = data.group.map(x =>
158
+
159
+ x.list.map(y => [y.sensor_group_id, x.uid])
160
+
161
+ ).flat();
162
+
163
+
164
+
165
+ console.log(pairs);
166
+
167
+
168
+
169
+ const groups = _(pairs)
170
+
171
+ .groupBy(0)
172
+
173
+ .mapValues(v => v.map(x => x[1]).sort((x1, x2) => x1 - x2))
174
+
175
+ .value();
176
+
177
+
178
+
179
+ console.log(groups);
180
+
181
+
182
+
183
+ const result = Object.entries(groups)
184
+
185
+ .sort(([x1], [x2]) => x1 - x2)
186
+
187
+ .map(([x, y]) => ({
188
+
189
+ sensor_group_id: +x,
190
+
191
+ list: y
192
+
193
+ }));
194
+
195
+
196
+
197
+ console.log(result);
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+ ```
206
+
207
+ - **動作確認用サンプル:** [https://codepen.io/jun68ykt/pen/RwWEpWb?editors=0012](https://codepen.io/jun68ykt/pen/RwWEpWb?editors=0012)

1

テキスト追加

2020/05/20 00:10

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -48,11 +48,11 @@
48
48
 
49
49
  ```javascript
50
50
 
51
- const result = pairs.reduce((ary, [gid, uid]) => {
51
+ const result = pairs.reduce((ary, [gid, uid], i) => {
52
52
 
53
- const x = ary.find(e => e.sensor_group_id === gid);
53
+ const x = i > 0 ? ary[ary.length - 1] : null;
54
54
 
55
- if (x) {
55
+ if (x && x.sensor_group_id === gid) {
56
56
 
57
57
  x.list.push(uid);
58
58
 
@@ -86,7 +86,7 @@
86
86
 
87
87
  ```
88
88
 
89
- - **動作確認用サンプル:** [https://codepen.io/jun68ykt/pen/gOaQNWy?editors=0012](https://codepen.io/jun68ykt/pen/gOaQNWy?editors=0012)
89
+ - **動作確認用サンプル:** [https://codepen.io/jun68ykt/pen/wvKQLYK?editors=0012](https://codepen.io/jun68ykt/pen/wvKQLYK?editors=0012)
90
90
 
91
91
 
92
92