I tried to figure this out for a long time, if I have an array of such objects:
var my_array = [
Object {Project: A, Hours: 2},
Object {Project: B, Hours: 3},
Object {Project: C, Hours: 5},
Object {Project: A, Hours: 6},
Object {Project: C, Hours: 9}
]
I want to combine all the objects with the same key together into one object, so that their time adds up:
Expected Result:
my_array = [
Object {Project: A, Hours: 8}
Object {Project: B, Hours: 3}
Object {Project: C, Hours: 14}
]
How can I approach this problem? It took me a long time for my data to be formatted in this way, this is the last step!
My attempt, I understand that I am sorting through an array, I donβt know how to deal with merging objects:
for (var i =0; i<my_array.length; i++) {
my_array[i].Project
my_array[i].Hours
}
source
share