0

What does this code mean?

const [votes, setVotes] = useState({ 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, }); const mostVoted = Object.keys(votes).sort((a, b) => votes[b] - votes[a])[0]; I don't understand what will be stored in mostVoted constant. Who can explain me what is the logic of this function? Thanks

15th Jul 2021, 6:15 PM
Elena
Elena - avatar
1 Answer
+ 4
Elena , first you get the keys => 0, 1, 2, 3, 4, 5; then you sort them in reverse order (5, 4, 3, 2, 1, 0), finally you get the first key in the sorting sequence => 5.
15th Jul 2021, 6:37 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar