Skip to content
Advertisement

how to add new calculate array object to existing array

I have an array

JavaScript

new calculate object :

Total1 = Phnom Penh + Takeo
Total 2 = Prey Veng + Seam Reap

then I want to add these two object to existing array (data)

JavaScript

Anyone please help me to do like this? Thanks

Advertisement

Answer

You could use a bespoke function that filters out the relevant objects, and then calculates their sum sales.

Here the data and an array of the locations is passed into getSales. The required objects are filtered out, and then reduce is used to sum their sales. You can then build a new object using the data from the old object, and adding in the new data.

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