I need to sort this from highest to lowest and limit the number of records to 10 with highest values, I'm not good with programming and couldn't find similar examples. Any tips regarding code would also be appreciated<33
function getMostFrequent(arr) {
const hashmap = arr.reduce( (acc, val) => {
acc[val] = (acc[val] || 0 ) + 1
return acc
},{})
return Object.entries(hashmap).sort((a, b) => b[1] - a[1]);
}
console.log(getMostFrequent(output));
this is the output I get
[
[ '300', 173 ], [ '200', 31 ], [ '500', 31 ],
[ '800', 29 ], [ '700', 28 ], [ '600', 26 ],
[ '400', 20 ], [ '290', 11 ], [ '160', 10 ],
[ '220', 10 ], [ '250', 9 ], [ '100', 8 ],
[ '240', 8 ], [ '260', 8 ], [ '350', 8 ],
[ '370', 8 ], [ '230', 7 ], [ '390', 7 ],
[ '210', 6 ], [ '280', 6 ], [ '110', 5 ],
[ '140', 5 ], [ '150', 5 ], [ '270', 5 ],
[ '310', 5 ], [ '320', 5 ], [ '120', 4 ],
[ '130', 4 ], [ '330', 4 ], [ '340', 4 ],
[ '170', 3 ], [ '190', 3 ], [ '360', 2 ],
[ '380', 2 ], [ '', 1 ]
]