JavaScript to split data and calculate sums

Viewed 487

I believe what I need are two JavaScript functions. I am receiving a comma separated string that holds two types of data: 1) device name followed by 2) numeric value. These two values are separated by a comma, and each set is also separated by a comma. Example string below:

Device_A,5,Device_C,2,Device_A,10,Device_B,8,Device_B,2,Device_C,7

What I want to do is create two separate functions. The first function finds the unique device names and returns just the names in a comma separated string. The second function would calculate the sum of the numeric values for each device. The expected results from the example string above would return:

Function 1 (Device List):

Device_A, Device_B, Device_C

Function 2 (Sums per Device List):

15,10,9

The lists do not need to return in any particular order as long at they both match up. All I have successfully done at this point is return a list of unique values (including numeric values)... I'm stuck on separating the list, but still referring to device name to sum up all of the values.

Thanks in advance. Let me know if you have any questions! Matt

4 Answers
Related