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

回答編集履歴

6

修正

2016/10/05 07:10

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -29,8 +29,8 @@
29
29
  {}
30
30
  );
31
31
 
32
- console.log(countToChars);
32
+ console.log(countToArrays);
33
- console.log(countToChars[2]);
33
+ console.log(countToArrays[2]);
34
34
  ```
35
35
 
36
36
  さらにBabelでコンパイルする前提なら, countToArrays の生成時に

5

修正

2016/10/05 07:10

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -33,7 +33,7 @@
33
33
  console.log(countToChars[2]);
34
34
  ```
35
35
 
36
- さらにBabelでコンパイルする前提なら, countToChars の生成時に
36
+ さらにBabelでコンパイルする前提なら, countToArrays の生成時に
37
37
 
38
38
  ```JavaScript
39
39
  const countToArrays = Object.entries(serialToCount).reduce(

4

修正

2016/10/05 07:05

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -11,19 +11,19 @@
11
11
  ["え","お"],
12
12
  ];
13
13
 
14
- const chars = [].concat(...groups);
14
+ const serials = groups.map(JSON.stringify);
15
15
 
16
- const charToCount = chars.reduce(
16
+ const serialToCount = serials.reduce(
17
- (map, char) => (
17
+ (map, serial) => (
18
- map[char] = (map[char] || 0) + 1,
18
+ map[serial] = (map[serial] || 0) + 1,
19
19
  map
20
20
  ),
21
21
  {}
22
22
  );
23
23
 
24
- const countToChars = Object.keys(charToCount).reduce(
24
+ const countToArrays = Object.keys(serialToCount).reduce(
25
- (map, char) => (
25
+ (map, serial) => (
26
- (map[charToCount[char]] = map[charToCount[char]] || []).push(char),
26
+ (map[serialToCount[serial]] = map[serialToCount[serial]] || []).push(JSON.parse(serial)),
27
27
  map
28
28
  ),
29
29
  {}
@@ -36,9 +36,9 @@
36
36
  さらにBabelでコンパイルする前提なら, countToChars の生成時に
37
37
 
38
38
  ```JavaScript
39
- const countToChars = Object.entries(charToCount).reduce(
39
+ const countToArrays = Object.entries(serialToCount).reduce(
40
- (map, [char, count]) => (
40
+ (map, [serial, count]) => (
41
- (map[count] = (map[count] || [])).push(char),
41
+ (map[count] = (map[count] || [])).push(JSON.parse(serial)),
42
42
  map
43
43
  ),
44
44
  {}

3

Babel

2016/10/05 07:04

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -45,4 +45,4 @@
45
45
  );
46
46
  ```
47
47
 
48
- として分割代入を使ったほうがきれいですね.
48
+ として`Object.entries`と分割代入を使ったほうがきれいですね.

2

Babel

2016/10/05 06:52

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -28,4 +28,21 @@
28
28
  ),
29
29
  {}
30
30
  );
31
+
32
+ console.log(countToChars);
33
+ console.log(countToChars[2]);
31
- ```
34
+ ```
35
+
36
+ さらにBabelでコンパイルする前提なら, countToChars の生成時に
37
+
38
+ ```JavaScript
39
+ const countToChars = Object.entries(charToCount).reduce(
40
+ (map, [char, count]) => (
41
+ (map[count] = (map[count] || [])).push(char),
42
+ map
43
+ ),
44
+ {}
45
+ );
46
+ ```
47
+
48
+ として分割代入を使ったほうがきれいですね.

1

冗長

2016/10/05 06:51

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -23,7 +23,7 @@
23
23
 
24
24
  const countToChars = Object.keys(charToCount).reduce(
25
25
  (map, char) => (
26
- (map[charToCount[char]] = (map[charToCount[char]] || [])).push(char),
26
+ (map[charToCount[char]] = map[charToCount[char]] || []).push(char),
27
27
  map
28
28
  ),
29
29
  {}