Skip to content
Advertisement

How to create hierarchy data in JavaScript by providing dynamic groups and summing the value?

I want to create a dynamic hierarchy based on my groups and rollup sum to top level, also if I am providing a large amount of data browser is getting hang.

I have the below data:

JavaScript

I have created below function but its not working properly

JavaScript

Required below sample result (I haven’t included other country but I need all)

JavaScript

Advertisement

Answer

Well, it is probably hangs because of O(n log n) (you iterate the same data over and over).

Note: priceEach is static in this solution, but you can redesign the recursive function

JavaScript

JSFiddle

If country, productLine, month are static, you can run the classic way

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