回答編集履歴

2

テキスト追加

2020/05/03 13:25

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -52,25 +52,17 @@
52
52
 
53
53
  ```javascript
54
54
 
55
+ const readers = ["first", "second"].map(file =>
56
+
55
- let data = {};
57
+ $.getJSON(`jsons/${file}.json`)
58
+
59
+ );
56
60
 
57
61
 
58
62
 
59
- const jsonReader = name =>
63
+ $.when(...readers).done((...args) => {
60
64
 
61
- $.getJSON(`jsons/${name}.json`, response => {
62
-
63
- data = { ...data, ...response };
64
-
65
- });
66
-
67
-
68
-
69
- const readers = ["first", "second"].map(jsonReader);
65
+ const data = Object.assign({}, ...args.map(arg => arg[0]));
70
-
71
-
72
-
73
- $.when(...readers).done(() => {
74
66
 
75
67
  $("#result").text(JSON.stringify(data, null, 4));
76
68
 
@@ -78,4 +70,4 @@
78
70
 
79
71
  ```
80
72
 
81
- - **動作確認用codesandbox:** [https://codesandbox.io/s/q258489-mjmh2](https://codesandbox.io/s/q258489-mjmh2)
73
+ - **動作確認用codesandbox:** [https://codesandbox.io/s/q258489-9eo3w](https://codesandbox.io/s/q258489-9eo3w)

1

テキスト追加

2020/05/03 13:25

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -37,3 +37,45 @@
37
37
 
38
38
 
39
39
  - **動作確認用codesandbox:** [https://codesandbox.io/s/q258489-yiqmi](https://codesandbox.io/s/q258489-yiqmi)
40
+
41
+
42
+
43
+
44
+
45
+ ### 追記
46
+
47
+
48
+
49
+ JQuery を使うのでしたら、 fetch や Promise.all を使わず、$.getJSON と $.when を使ってもできます。
50
+
51
+
52
+
53
+ ```javascript
54
+
55
+ let data = {};
56
+
57
+
58
+
59
+ const jsonReader = name =>
60
+
61
+ $.getJSON(`jsons/${name}.json`, response => {
62
+
63
+ data = { ...data, ...response };
64
+
65
+ });
66
+
67
+
68
+
69
+ const readers = ["first", "second"].map(jsonReader);
70
+
71
+
72
+
73
+ $.when(...readers).done(() => {
74
+
75
+ $("#result").text(JSON.stringify(data, null, 4));
76
+
77
+ });
78
+
79
+ ```
80
+
81
+ - **動作確認用codesandbox:** [https://codesandbox.io/s/q258489-mjmh2](https://codesandbox.io/s/q258489-mjmh2)