回答編集履歴

3

修正

2022/09/03 17:51

投稿

退会済みユーザー
test CHANGED
@@ -29,7 +29,7 @@
29
29
 
30
30
  ### 追記
31
31
 
32
- [lodash](https://lodash.com/) を使うと、以下によって`ary`から`total`を得られます。
32
+ lodashの [_.mergeWith](https://lodash.com/docs/#mergeWith) を使うと、以下によって`ary`から`total`を得られます。
33
33
  ```javascript
34
34
  const total = ary.reduce((s, e) => _.mergeWith(s, e, (x=0, y) => x + y), {});
35
35
  ```

2

修正

2022/09/03 17:16

投稿

退会済みユーザー
test CHANGED
@@ -31,6 +31,6 @@
31
31
 
32
32
  [lodash](https://lodash.com/) を使うと、以下によって`ary`から`total`を得られます。
33
33
  ```javascript
34
- const total = ary.reduce((s, e) => _.mergeWith(s, e, (x, y) => _.sum([x, y])), {});
34
+ const total = ary.reduce((s, e) => _.mergeWith(s, e, (x=0, y) => x + y), {});
35
35
  ```
36
- - 👉 https://codepen.io/su507/pen/MWGWdMq?editors=0012
36
+ - 👉 https://codepen.io/su507/pen/ExLxBQj?editors=0012

1

追記

2022/09/03 16:49

投稿

退会済みユーザー
test CHANGED
@@ -27,3 +27,10 @@
27
27
 
28
28
  質問にあるコードに上記を使おうとすれば、まずは`mealData`の各要素の `ingredience` を要素とする配列を作ってそれに上記のコードを適用するとよいでしょう。
29
29
 
30
+ ### 追記
31
+
32
+ [lodash](https://lodash.com/) を使うと、以下によって`ary`から`total`を得られます。
33
+ ```javascript
34
+ const total = ary.reduce((s, e) => _.mergeWith(s, e, (x, y) => _.sum([x, y])), {});
35
+ ```
36
+ - 👉 https://codepen.io/su507/pen/MWGWdMq?editors=0012