765 B
765 B
title, tags, firstSeen, lastUpdated
| title | tags | firstSeen | lastUpdated |
|---|---|---|---|
| differenceBy | array,intermediate | 2018-01-24T11:49:03+02:00 | 2020-10-19T18:52:00+03:00 |
Returns the difference between two arrays, after applying the provided function to each array element of both.
- Create a
Setby applyingfnto each element inb. - Use
Array.prototype.map()to applyfnto each element ina. - Use
Array.prototype.filter()in combination withfnonato only keep values not contained inb, usingSet.prototype.has().
const differenceBy = (a, b, fn) => {
const s = new Set(b.map(fn));
return a.map(fn).filter(el => !s.has(el));
};
differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [1]
differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x); // [2]