In this Article we will go through how to get the rank of an array of numbers only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function
.
Let's define this short function:
const ranking = (arr) => arr.map((x, y, z) => z.filter(w => w > x).length + 1);
ranking([80, 65, 90, 50]); // [2, 3, 1, 4]
ranking([80, 80, 70, 50]); // [1, 1, 3, 4]
ranking([80, 80, 80, 50]); // [1, 1, 1, 4]