var sortedArr = [["Paul", "1355"],
["Jennifer", "1910"],
["John", "835"],
["John", "830"],
["Paul", "1315"],
["John", "1615"],
["John", "1640"],
["Paul", "1405"],
["John", "855"],
["John", "930"],
["John", "915"],
["John", "730"],
["John", "940"],
["Jennifer", "1335"],
["Jennifer", "730"],
["John", "1630"],
["Jennifer", "5"]
];
I am trying to find the total number of counts for each key, For example, Paul appears three times so Paul should count 3 times.
and then want to write all the values for it. For example for Paul value should be 1355 1315 1405
It should be Like Paul 1355, 1315, 1405
I try something, but I get some issues, the issue are more about logic. Can someone please guide me?
count = 1;
var test;
//sortedArr = arr.sort();
for (var i = 0; i < sortedArr.length; i = i + count) {
count = 1;
test = [];
test.push(sortedArr[i][1]);
for (var j = i + 1; j < sortedArr.length; j++) {
if (sortedArr[i][0] === sortedArr[j][0])
{
count++;
//
test.push(sortedArr[j][1]);
}
}
document.write(sortedArr[i][0] +test +" = " + count + "<br>");
}
Output (wrong)
Paul1355,1315,1405 = 3
John830,1615,1640,855,930,915,730,940,1630 = 9
John940,1630 = 2
Jennifer730,5 = 2
Jennifer5 = 1
Output should be
Paul1355,1315,1405 = 3
John835, 830,1615,1640,855,930,915,730,940,1630 = 10
Jennifer1910, 1335, 730,5 = 4
Can someone tell me the issue in my code?