Menu

gitpiper

differenceBy javascript Code Snippet in 2024

arrayintermediate

Last Updated: 5 October 2024

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.
  • Use Array.prototype.map() to apply fn to each element in a.
  • Use Array.prototype.filter() in combination with fn on a to only keep values not contained in b, using Set.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]

javascript snippet similar to differenceBy For You in October 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️