Calculating which color holds a higher value in each array element of the data. Then adding the color with the higher value to an empty object, or increasing the count if already present. Finally, sorting the totals object from highest to lowest based on the total values and returning the color with the highest value.
Facing challenges mapping over this structured array as the property keys are not consistent. Should I consider restructuring it?
*I am open to redesigning the data structure if necessary, so please suggest if there's an easier solution with a different design!
data = [
{ orange: 4, green: 4},
{ green: 0, yellow: 0},
{ yellow: 1, orange: 4 },
{ blue: 2, green: 1 },
{ blue: 2, yellow: 1 },
{ green: 3, yellow: 2 },
{ green: 1, blue: 3},
{ green: 5, yellow: 2 },
]
totals = {
blue: 3,
green: 2,
orange: 1,
}
Solution: highValueColor = blue
// PSEUDOCODE //map over the array => data.map() //identify highest value between two elements => propA - propB //check to see if the color's (key) in the element has already been added to totals object //IF the key does not yet exist, create a property in the tally object with the color(key) and set its value to 1 //IF the key is already listed in tally object, increment its property value by 1 => ++ //sort totals object => Math.max() //return highest value color