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

質問編集履歴

2

質問内容を元に戻しました。

2021/12/11 01:08

投稿

nanase21
nanase21

スコア144

title CHANGED
@@ -1,1 +1,1 @@
1
- 動的に増えるデータに対する型定義方法につい知りた
1
+ 動的にobjectを追加していく方法
body CHANGED
@@ -1,7 +1,17 @@
1
1
  # 実現したいこと
2
2
 
3
+ ```json
4
+ [
5
+ {id: 1, title: 'test1', createdAt: '2021-12-10T00:00:00.001Z'},
6
+ {id: 2, title: 'test2', createdAt: '2021-12-11T00:00:00.001Z'},
7
+ {id: 3, title: 'test3', createdAt: '2021-12-12T00:00:00.001Z'},
8
+ {id: 4, title: 'test3', createdAt: '2021-12-12T00:00:00.001Z'},
9
+ ]
10
+ ```
3
- 以下のデータ構造の型定義方法を知りたい。
11
+ 上記ようなデータ構造があったときに、下記ようなデータ構造に整形する方法を知りたい。
4
12
 
13
+ 理想のデータ構造
14
+
5
15
  ```js
6
16
  {
7
17
    '2021-12-10': { marked: true },
@@ -11,14 +21,46 @@
11
21
  }
12
22
  ```
13
23
 
14
- ```ts
24
+ # 現状できていること
15
- // {[key: string;]: { marked: boolean}}
16
- keyが動的の場合の型付けは上記の方法で出来るのですが、APIのレスポンスによって数が変わる場合の定義方法が分からなくて困っています。
17
25
 
26
+ 以下のようなコードで、配列型でそれっぽくは出来たのですが、上記のような「理想のデータ構造」にする方法が分からず困っています。
27
+
28
+ ```js
29
+ const arr = data.map((e) => {
18
- const test: {[key: string;]: { marked: boolean} } = {
30
+ const created_at = moment(e.created_at).format('YYYY-MM-DD');
31
+ return {
19
-   '2021-12-10': { marked: true },
32
+ [created_at]: { marked: true },
20
-   '2021-12-11': { marked: true },
21
-   '2021-12-12': { marked: true },
22
-   '2021-12-13': { marked: true },
23
- };
33
+ };
34
+ });
35
+
36
+ console.log(arr);
37
+ /*
38
+ Array [
39
+ Object {
40
+ "2021-12-10": Object {
41
+ "marked": true,
42
+ },
43
+ },
44
+ Object {
45
+ "2021-12-11": Object {
46
+ "marked": true,
47
+ },
48
+ },
49
+ Object {
50
+ "2021-12-12": Object {
51
+ "marked": true,
52
+ },
53
+ },
54
+ Object {
55
+ "2021-11-01": Object {
56
+ "marked": true,
57
+ },
58
+ },
59
+ Object {
60
+ "2021-12-13": Object {
61
+ "marked": true,
62
+ },
63
+ },
64
+ ],
65
+ */
24
66
  ```

1

質問の内容の変更

2021/12/11 01:08

投稿

nanase21
nanase21

スコア144

title CHANGED
@@ -1,1 +1,1 @@
1
- 動的にobjectを追加していく方法
1
+ 動的に増えるデータに対する型定義方法について知りたい
body CHANGED
@@ -1,17 +1,7 @@
1
1
  # 実現したいこと
2
2
 
3
- ```json
4
- [
5
- {id: 1, title: 'test1', createdAt: '2021-12-10T00:00:00.001Z'},
6
- {id: 2, title: 'test2', createdAt: '2021-12-11T00:00:00.001Z'},
7
- {id: 3, title: 'test3', createdAt: '2021-12-12T00:00:00.001Z'},
8
- {id: 4, title: 'test3', createdAt: '2021-12-12T00:00:00.001Z'},
9
- ]
10
- ```
11
- 上記のようなデータ構造があったときに、ようなデータ構造に整形する方法を知りたい。
3
+ 下のデータ構造の型定義方法を知りたい。
12
4
 
13
- 理想のデータ構造
14
-
15
5
  ```js
16
6
  {
17
7
    '2021-12-10': { marked: true },
@@ -21,46 +11,14 @@
21
11
  }
22
12
  ```
23
13
 
24
- # 現状できていること
14
+ ```ts
15
+ // {[key: string;]: { marked: boolean}}
16
+ keyが動的の場合の型付けは上記の方法で出来るのですが、APIのレスポンスによって数が変わる場合の定義方法が分からなくて困っています。
25
17
 
26
- 以下のようなコードで、配列型でそれっぽくは出来たのですが、上記のような「理想のデータ構造」にする方法が分からず困っています。
27
-
28
- ```js
29
- const arr = data.map((e) => {
30
- const created_at = moment(e.created_at).format('YYYY-MM-DD');
18
+ const test: {[key: string;]: { marked: boolean} } = {
31
- return {
32
- [created_at]: { marked: true },
19
+   '2021-12-10': { marked: true },
20
+   '2021-12-11': { marked: true },
21
+   '2021-12-12': { marked: true },
22
+   '2021-12-13': { marked: true },
33
- };
23
+ };
34
- });
35
-
36
- console.log(arr);
37
- /*
38
- Array [
39
- Object {
40
- "2021-12-10": Object {
41
- "marked": true,
42
- },
43
- },
44
- Object {
45
- "2021-12-11": Object {
46
- "marked": true,
47
- },
48
- },
49
- Object {
50
- "2021-12-12": Object {
51
- "marked": true,
52
- },
53
- },
54
- Object {
55
- "2021-11-01": Object {
56
- "marked": true,
57
- },
58
- },
59
- Object {
60
- "2021-12-13": Object {
61
- "marked": true,
62
- },
63
- },
64
- ],
65
- */
66
24
  ```