differenceBy
difference, comparing by a computed key instead of value equality.
Lecture
differenceBy(f, iterable1, iterable2) follows the exact
same argument-order rule as difference:
the result comes from iterable2, keeping
only the elements whose f-key does not appear
among iterable1's f-keys (with duplicates in
the result collapsed). The f function is applied to
both iterables, so — unlike differenceBy in some
languages — both arguments must share the same element type
A; only the derived key type B is what's being
compared.
This is the shape you reach for when the "exclusion list" and the
"source list" are full records rather than bare values: a list of
blocked user objects and a list of user objects, matched by
id; or an unavailable-products list and a product catalog,
matched by sku.
difference itself is just
differenceBy((a) => a, iterable1, iterable2). There's no
chain method for either — call the data-first function directly. On the
async side, the concurrency marker applies to iterable2,
same as differenceAsync.
Demo 1 · Basics
Demo 2 · Async, with concurrency
Try it yourself
Exercise: use differenceBy (keyed on 'sku')
to find products that are still available.
difference — the value-equality version ·
intersectionBy — keep by a shared computed key instead ·
uniqBy — dedupe a single iterable by key ·
compress — filter by a parallel boolean mask