Write a function named uniqBy
that will be used to extend the Array prototype and allow us to remove duplicates from an Array based on a specific property.
The function will receive another function as a parameter - let's call it identity
- that will receive an element of the Array as a parameter, and return a value.
Based on this value we'll remove duplicates from the Array. The first occurrence of the element will be kept, and all subsequent others will be removed.
Array.prototype.uniqBy = uniqBy;
const numbers = [
10, 11, 12, 13, 14, 15,
20, 21, 22, 23, 24, 25,
30, 31, 32, 33, 34, 35,
];
const uniqNumbersPerInterval = numbers.uniqBy(
(number) => Math.floor(number / 10)
);
console.log(uniqNumbersPerInterval); // [10, 20, 30]