Skip to content

Latest commit

 

History

History
45 lines (36 loc) · 1.06 KB

distinctBy.md

File metadata and controls

45 lines (36 loc) · 1.06 KB

distinctBy

Used to create a new collection with the distinct values, using a custom comparator.

For simple types or doing a deep equals on complex types, use distinct instead.

Syntax:
distinctBy(compareFn)
Usage in a reducer:
array.reduce(distinctBy(compareFn))

Arguments:

  • compareFn is a used to determine whether two elements are equal.
    compareFn is of the form (left, right) => boolean

Usage:

const testData = [
    { p1: 10, p2: 'a' },
    { p1: 10, p2: 'a' },
    { p1: 20, p2: 'a' },
    { p1: 20, p2: 'b' }
]

const res = testData.reduce(distinctBy((left, right) => left.p1 === right.p1 && left.p2 === right.p2), [])

// Result:
// [
//     { p1: 10, p2: 'a' },
//     { p1: 20, p2: 'a' },
//     { p1: 20, p2: 'b' }
// ]

Remarks:

const res = testData.reduce(distinctBy((left, right) => left.p1 === right.p1 && left.p2 === right.p2), [])

See also: