Mapped array union

JavaScript, Array · Oct 22, 2020

Returns every element that exists in any of the two arrays at least once, after applying the provided function to each array element of both.

  • Create a Set by applying all fn to all values of a.
  • Create a Set from a and all elements in b whose value, after applying fn does not match a value in the previously created set.
  • Return the last set converted to an array.
const unionBy = (a, b, fn) => {
  const s = new Set(a.map(fn));
  return Array.from(new Set([...a, ...b.filter(x => !s.has(fn(x)))]));
};

unionBy([2.1], [1.2, 2.3], Math.floor); // [2.1, 1.2]
unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], x => x.id)
// [{ id: 1 }, { id: 2 }, { id: 3 }]

More like this

  • JavaScript Array Set Operations

    Learn how to apply mathematical set operations to JavaScript arrays.

    Collection · 12 snippets

  • Mapped array intersection

    Returns the elements that exist in both arrays, after applying the provided function to each array element of both.

    JavaScript, Array · Oct 20, 2020

  • Mapped array symmetric difference

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

    JavaScript, Array · Oct 22, 2020

  • Mapped array difference

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

    JavaScript, Array · Oct 19, 2020