Write uniqueBy(array, key) to remove duplicates by a computed key, keeping the first of each.
key is either a function that returns an item's key or a property name. Compare keys the way a Set does (SameValueZero, so NaN matches NaN). Keep the original order and don't mutate the input.
function uniqueBy(array, key) {
// your code
}
// uniqueBy([{ id: 1, v: "a" }, { id: 2 }, { id: 1, v: "b" }], "id"); // [{ id: 1, v: "a" }, { id: 2 }]
// uniqueBy([1.2, 1.8, 2.1], Math.floor); // [1.2, 2.1]
Solve it in the editor, with tests
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…