I have an array, that looks like this:
colors = [
[356, 11, 30, 1],
[354, 10, 1, 1],
[220, 15, 33, 14],
[51, 4, 69, 31],
[38, 4, 54, 32],
[33, 53, 52, 33],
[19, 28, 78, 34],
[0, 0, 27, 36]
];
It is an array of HSL colors with colors[0]
, colors[1]
, colors[2]
representing HSL values and colors[3]
representing the group that this color belongs to. I have 36 groups of colors.
I want to be able to get the frequency of colors in each group (so count of color[3]
) and also sum the colors[1]
of each group.
Could you help me setup my code? I’ve tried making an object with keys for every group to make this easier to work with, but couldn’t ...
Here's the code I was trying to transform my array and make it easier to make the sum and frequency calculations:
hueGroups = {};
for (var i = 0;i<colors.length-1;i++) {
var group = colors[i][3];
hueGroups[group] = colors[i];
}
And it gives me:
{ '1': [ 354, 10, 1, 1 ],
'14': [ 220, 15, 33, 14 ],
'31': [ 51, 4, 69, 31 ],
'32': [ 38, 4, 54, 32 ],
'33': [ 33, 53, 52, 33 ],
'34': [ 19, 28, 78, 34 ] }
I also would prefer not using external libraries, I’ve seen solutions using Underscore.js.