typestar

Unique by key in JavaScript

Deduplicates by a computed key rather than by identity.

function uniqueBy(items, keyFn) {
  const seen = new Set();
  return items.filter((item) => {
    const key = keyFn(item);
    if (seen.has(key)) return false;
    seen.add(key);
    return true;
  });
}

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
181
Tokens
60
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 40 seconds.

Type this snippet

Step 6 of 10 in Arrays, step 22 of 43 in Language basics.

← Previous Next →

Unique by key in other languages