Currently, I got an array like that:
uniqueCount = [a,a,b,c,d,a,a];
How can I count how many a,b,c are there in the array? I want to have a result like with format of array of object:
[{key: "a", count: 4}
{key: "b", count: 1}
{key: "c", count: 1}
{key: "d", count: 1}]
Mycode :
var current = null;
var count = 0;
for (var i = 0; i < uniqueCount .length; i++) {
if (uniqueCount [i] != current) {
if (count > 0) {
result.push({
key: current,
count: count
});
}
current = uniqueCount [i];
count = 1;
} else {
count++;
}
}
if (count > 0) {
result.push({
key: current,
count: count
});
}
But the result:
[{key: "a", count: 2}
{key: "b", count: 1}
{key: "c", count: 1}
{key: "d", count: 1}
{key: "a", count: 2}]
Thanks you .