Array.prototype.map()
to apply fn
to all elements in arr
.Set
from the mapped values to keep only unique occurrences.Array.prototype.length
and Set.prototype.size
to compare the length of the unique mapped values to the original array.const allUniqueBy = (arr, fn) => arr.length === new Set(arr.map(fn)).size;
allUniqueBy([1.2, 2.4, 2.9], Math.round); // true allUniqueBy([1.2, 2.3, 2.4], Math.round); // false
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️