Hi,
Let’s say I have an array like this:
var patients=[{city=”London”, score=172}, {city=”London”, score=178}, {city=”Rome”, score=158}, {city=”Paris”, score=17}]
How can i find which city is most and least repeated in an array ?
#javascript
const patients = [ { city: "London", score: 172 }, { city: "London", score: 178 }, { city: "Rome", score: 158 }, { city: "Paris", score: 17 }, { city: "Paris", score: 20 }, { city: "Paris", score: 25 } ]; let result = {}; patients.forEach((item, idx) => { if (!result[item.city]) { result[item.city] = 1; } else { result[item.city]++; } }); let result2 = []; for (city in result) { result2.push({ city: city, nr: result[city] }); } result2.sort((a, b) => a.nr - b.nr); console.log(result); console.log(result2);
Help @albionshalaa spread the word by sharing this article on Twitter...