FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Union By

25mins

Implement a function unionBy(arrays, iteratee) that merges multiple arrays into one, keeping only unique values, determined by a transformation function or property name.

The function should:

  • Accept multiple arrays and an iteratee.
  • The iteratee can be either:
    • A function applied to each element to determine uniqueness.
    • A string representing a property key to check uniqueness.
  • Preserve the order of the first appearance of elements.

Examples

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

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests