I've been struggling to figure this out on my own, so I decided to seek advice from those with more experience. I have an array of objects called items, and I need to sum up specific properties across different objects in the array. The user can select certain properties, and I need to only sum those up based on their selections. My initial thought was to use the _.pick function in lodash for this task. Ideally, I would like to accomplish this in just one loop since the items array could potentially contain up to a thousand items. Here's an example:
var items = [
{'lightBlue':4, 'darkBlue':2, 'red':4, 'orange':6, 'purple':7},
{'lightBlue':6, 'darkBlue':5, 'red':1, 'orange':2, 'purple':3},
{'lightBlue':2, 'darkBlue':4, 'red':3, 'orange':4, 'purple':9}
]
var userSelectedColors = ['lightBlue', 'darkBlue'];
The desired output is the summation of all selected colors (in this case, blue):
var summedUp = [{'lightBlue':12, 'darkBlue':11}];
Finally, adding up these results gives us the total count:
var totalCount = 23;
What is the most efficient way to achieve this using lodash? Keep in mind that the userSelectedColors array could have any combination of colors chosen.
Please provide an example. Any help or suggestions are greatly appreciated!