Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 1.05 KB

differenceBy.md

File metadata and controls

28 lines (23 loc) · 1.05 KB
title tags author_title author_url author_image_url description image
differenceBy
array,function,intermediate
Deepak Vishwakarma
Implementation of "differenceBy" in typescript, javascript and deno.

JS TODO

Returns the difference between two arrays, after applying the provided function to each array element of both.

Create a Set by applying fn to each element in b, then use Array.prototype.map() to apply fn to each element in a, then Array.prototype.filter()

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]