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

回答編集履歴

1

テキスト修正

2020/08/19 03:39

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -8,4 +8,17 @@
8
8
  - arr.address = address[i];
9
9
  + arr.push({ id: id[i], name: name[i], address: address[i] });
10
10
  ```
11
- - **動作確認用サンプル:** [https://codepen.io/jun68ykt/pen/GRZjoYK](https://codepen.io/jun68ykt/pen/GRZjoYK?editors=0012)
11
+ - **動作確認用サンプル:** [https://codepen.io/jun68ykt/pen/GRZjoYK](https://codepen.io/jun68ykt/pen/GRZjoYK?editors=0012)
12
+
13
+ ### 追記
14
+
15
+ 配列やオブジェクトの操作で便利なライブラリ lodash の [_.zipWith](https://lodash.com/docs/#zipWith) を使うと、以下のように書けます。
16
+
17
+ ```javascript
18
+ const ids = [1, 2, 3, 4];
19
+ const names = ['A', 'B', 'C', 'A'];
20
+ const addresses = ['a', 'b', 'c', 'a'];
21
+
22
+ const arr = _.zipWith(ids, names, addresses, (id, name, address) => ({ id, name, address }));
23
+ ```
24
+ - **動作確認用サンプル:** [https://codepen.io/jun68ykt/pen/yLOaeQX](https://codepen.io/jun68ykt/pen/yLOaeQX?editors=0012)