回答編集履歴
2
letでなくてconstに直した...ねむい
test
CHANGED
@@ -2,35 +2,31 @@
|
|
2
2
|
|
3
3
|
```js
|
4
4
|
|
5
|
-
let data = [["a", "b", 10], ["a", "b", 20], ["a", "c", 30], ["a", "c", 40], ["a", "d", 50], ["a", "b", "c",50], ["a", "b", "c",50]];
|
5
|
+
let data = [["a", "b", 10], ["a", "b", 20], ["a", "c", 30], ["a", "c", 40], ["a", "d", 50], ["a", "b", "c", 50], ["a", "b", "c", 50]];
|
6
6
|
|
7
7
|
//[["a","b",30],["a","c",70],["a","d",50];
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
|
11
|
+
const aggregate = new Map();
|
12
12
|
|
13
13
|
data.forEach(keys_value => {
|
14
14
|
|
15
|
-
|
15
|
+
const value_keys = Array.from(keys_value);
|
16
16
|
|
17
17
|
value_keys.unshift(value_keys.pop());
|
18
18
|
|
19
19
|
const [value, ...keys] = [...value_keys];
|
20
20
|
|
21
|
-
aggregate.set(JSON.stringify(keys), (aggregate.get(JSON.stringify(keys)) || 0)+value);
|
21
|
+
aggregate.set(JSON.stringify(keys), (aggregate.get(JSON.stringify(keys)) || 0) + value);
|
22
22
|
|
23
23
|
});
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
const result = [...aggregate.entries()].map(v => [...JSON.parse
|
27
|
+
const result = [...aggregate.entries()].map(v => [...JSON.parse(v[0]), v[1]]); //[ [ "a", "b", 30 ], [ "a", "c", 70 ], [ "a", "d", 50 ], [ "a", "b", "c", 100 ] ]
|
28
28
|
|
29
29
|
console.log(result);
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
30
|
|
35
31
|
```
|
36
32
|
|
1
keysの方がしっくり来る
test
CHANGED
@@ -10,15 +10,15 @@
|
|
10
10
|
|
11
11
|
let aggregate = new Map();
|
12
12
|
|
13
|
-
data.forEach(key_value => {
|
13
|
+
data.forEach(keys_value => {
|
14
14
|
|
15
|
-
let value_key = Array.from(key_value);
|
15
|
+
let value_keys = Array.from(keys_value);
|
16
16
|
|
17
|
-
value_key.unshift(value_key.pop());
|
17
|
+
value_keys.unshift(value_keys.pop());
|
18
18
|
|
19
|
-
const [value, ...key] = [...value_key];
|
19
|
+
const [value, ...keys] = [...value_keys];
|
20
20
|
|
21
|
-
aggregate.set(JSON.stringify(key), (aggregate.get(JSON.stringify(key)) || 0)+value);
|
21
|
+
aggregate.set(JSON.stringify(keys), (aggregate.get(JSON.stringify(keys)) || 0)+value);
|
22
22
|
|
23
23
|
});
|
24
24
|
|
@@ -27,6 +27,8 @@
|
|
27
27
|
const result = [...aggregate.entries()].map(v => [...JSON.parse (v[0]),v[1]]); //[ [ "a", "b", 30 ], [ "a", "c", 70 ], [ "a", "d", 50 ], [ "a", "b", "c", 100 ] ]
|
28
28
|
|
29
29
|
console.log(result);
|
30
|
+
|
31
|
+
|
30
32
|
|
31
33
|
|
32
34
|
|