Policies & Guidelines

UNIQUE_BY_KEY

Find unique objects by a key

Description

Returns the original array where any object duplicates by the given key are removed.

For example, given an array of objects and Key id, would return an array where items that have duplicate id property values are removed.

The object with the first unique value is always picked into the returned array, with subsequent occurences of the same value treated as duplicates and discarded.

Function signature

UNIQUE_BY_KEY(array: Array, key: String) => Array

Parameter

Description

Array

Array to find objects

Key

Key for compare

Examples

array1 = [{"a":3},{"a":2},{"a":2,"b":3},{"a":2,"b":5}]
key = "a"
array2 = [{"a":{"b":20}},{"a":{"b":10}},{"a":{"b":10}}]

Formula

Return value

UNIQUE_BY_KEY(array1, key)

[{"a":3},{"a":2}]

UNIQUE_BY_KEY(array2, key)

[{"a":{"b":20}},{"a":{"b":10}}]