Skip to content

Function: uniqBy()

ts
function uniqBy<T, K>(array, iteratee): T[];

Removes duplicate items from an array using a derived key. 按派生键对数组去重。

Type Parameters

Type ParameterDescription
TThe type of array items / 数组元素类型
KThe derived key type / 派生键类型

Parameters

ParameterTypeDescription
arrayreadonly T[]The source array / 源数组
iterateeUniqByIteratee<T, K>The property name or function used to derive the uniqueness key / 用于生成唯一键的属性名或函数

Returns

T[]

A new array containing the first item for each unique key / 每个唯一键保留首个元素的新数组

Example

ts
uniqBy([{ id: 1 }, { id: 1 }, { id: 2 }], 'id')
// => [{ id: 1 }, { id: 2 }]

Since

1.2.0

Released under the MIT License.