In my JavaScript code, I am facing a challenge that I have been struggling to solve. I have attempted various methods, but none have proven successful for me so far. The situation is as follows: I have two arrays of equal length. For example:
var years = [2010, 2011, 2009, 2008, 2010, 2011, 2007, 2008, 2008];
var money = [2, 3, 6, 5, 13, 8, 3, 9, 7];
Each element in the money
array corresponds to an element at the same index in the years
array, representing funds spent in that year. My goal is to create a new array of distinct years (removing duplicate values from the original years
array) and a new money
array with elements that are sums of the initial money values associated with each year.
years_new = [2010, 2011, 2009, 2008, 2007];
money_new = [2 + 13, 3 + 8, 6, 5 + 9 + 7, 3];
I'm looking for guidance on how to achieve this task efficiently. Any help would be greatly appreciated. Thank you!