⚖️ Returns a new array containing elements that are in either collection, but not in both (symmetric difference).

Info

It uses isEql for deep comparison of values.

Venn Diagram

Syntax

TypeScript
import { symDiff } from '@opentf/std';

symDiff(
  collections: unknown[][] = [],
  by?: (val: unknown) => unknown
): unknown[]

Parameters

  • collections: An array of arrays to compare.

  • by: An optional iteratee invoked for each element to generate the criterion by which symmetric difference is computed.

Returns

A new array containing the unique symmetric difference of the collections.

Examples

TypeScript
const evens = [2, 4, 6, 8];
const squares = [1, 4, 9];
symDiff([evens, squares]) //=> [2, 6, 8, 1, 9]

const bucket1 = ['Fruits', 'Vegs', 'Eggs', 'Cookies', 'Nuts'];
const bucket2 = ['fruits', 'Snacks', 'cookies'];
symDiff([bucket1, bucket2], (v) => v.toLowerCase()) 
//=> [
//  'Vegs',
//  'Eggs',
//  'Nuts',
//  'Snacks',
// ]
Last updated on
Edit this page