回答編集履歴
2
テキスト修正
test
CHANGED
@@ -54,9 +54,7 @@
|
|
54
54
|
|
55
55
|
```javascript
|
56
56
|
|
57
|
-
const zip = (ary1, ary2) =>
|
58
|
-
|
59
|
-
ary1.map((e, i) =>
|
57
|
+
const zip = (ary1, ary2) => ary1.map((e, i) => [e, ary2[i]]);
|
60
58
|
|
61
59
|
```
|
62
60
|
|
1
テキスト修正
test
CHANGED
@@ -20,7 +20,81 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
- 動作確認用 CodePen: [https://codepen.io/jun68ykt/pen/VwwVBPx?editors=0012](https://codepen.io/jun68ykt/pen/VwwVBPx?editors=0012)
|
23
|
+
- **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/VwwVBPx?editors=0012](https://codepen.io/jun68ykt/pen/VwwVBPx?editors=0012)
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
### 別解
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
lodash を使わない案も挙げておきます。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
まず、以下の2つの配列
|
36
|
+
|
37
|
+
- `[ "name", "address", "tel" ]`
|
38
|
+
|
39
|
+
- `[ "田中", "東京", "000000000" ]`
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
から、下記の配列
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
- `[ [ "name", "田中" ], [ "address", "東京" ], [ "tel", "000000000" ] ]`
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
を得る関数を作ります。このような処理をする関数は、慣例的に zip と呼ばれるので、それに倣います。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
```javascript
|
56
|
+
|
57
|
+
const zip = (ary1, ary2) =>
|
58
|
+
|
59
|
+
ary1.map((e, i) => ([e, ary2[i]]));
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
上記の zip によって得られる
|
66
|
+
|
67
|
+
`[ [ "name", "田中" ], [ "address", "東京" ], [ "tel", "000000000" ] ]`
|
68
|
+
|
69
|
+
という形式の配列を
|
70
|
+
|
71
|
+
- [Object.fromEntries()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
に渡せば、オブジェクトが作られます。以下はこれを使ったコードです。
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
```javascript
|
82
|
+
|
83
|
+
const objlist = valuelist.map(v =>
|
84
|
+
|
85
|
+
Object.fromEntries(zip(key, v))
|
86
|
+
|
87
|
+
);
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
- **動作確認用 CodePen:** [https://codepen.io/jun68ykt/pen/MWWzBVb?editors=0012](https://codepen.io/jun68ykt/pen/MWWzBVb?editors=0012)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
24
98
|
|
25
99
|
|
26
100
|
|