Skip to content
Advertisement

Compare array of object values if equals add it total

JavaScript

I have an array structure as above in my app whenever user clicks make pizza or another recipe it adding recipe informations to new menu state given below.

JavaScript

After that i need to compare that menu array’s ingName and unit fields to calculate total ingredient need for ex. according to above array total is like

JavaScript

I tried this :

JavaScript

it gets ingName property but after that i stuck.Thanks for help.

Edit :

React Solution : First declared Shyam’s solution as function and used useeffect hook for to calculate total ingredients when user clicks button, useeffect updating total array.

JavaScript

And modified button

JavaScript

Used indexCount state to locate last added recipe’s index.

Advertisement

Answer

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement