Start of main content## Recommended snippets

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.

```
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 }]
```

### Mapped array symmetric difference

JavaScript, Array

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

### Mapped array difference

JavaScript, Array

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

### Array union based on function

JavaScript, Array

Returns every element that exists in any of the two arrays at least once, using a provided comparator function.