uniqBy
uniqBy() function
Unlike uniq returns Iterable/AsyncIterable with duplicate values removed by applying with f
inside the given Iterable/AsyncIterable. The order of result values is determined by the order they occur in the array.
declare function uniqBy<A, B>(f: (a: A) => B, iterable: Iterable<A>): IterableIterator<A>;
declare function uniqBy<A, B>(f: (a: A) => B, iterable: AsyncIterable<A>): AsyncIterableIterator<A>;
declare function uniqBy<A extends Iterable<unknown> | AsyncIterable<unknown>, B>(f: (a: IterableInfer<A>) => B, iterable?: A): (iterable: A) => ReturnIterableIteratorType<A>;
Example
const iter = uniqBy(a => a.age, [{age: 21}, {age: 23}, {age: 21}, {age: 34}]);
iter.next() // {done:false, value: {age: 21}}
iter.next() // {done:false, value: {age: 23}}
iter.next() // {done:false, value: {age: 34}}
iter.next() // {done:true, value: undefined}
// with pipe
pipe(
[{age: 21}, {age: 23}, {age: 21}, {age: 34}],
uniqBy(a => a.age),
toArray,
); // [{age: 21}, {age: 23}, {age: 34}]
await pipe(
Promise.resolve([{age: 21}, {age: 23}, {age: 21}, {age: 34}]),
uniqBy(a => a.age),
toArray,
); // [{age: 21}, {age: 23}, {age: 34}]
// if you want to use asynchronous callback
await pipe(
Promise.resolve([{age: 21}, {age: 23}, {age: 21}, {age: 34}]),
toAsync,
uniqBy(async (a) => a.age),
toArray,
); // [{age: 21}, {age: 23}, {age: 34}]
// toAsync
await pipe(
[Promise.resolve({age: 21}), Promise.resolve({age: 23}), Promise.resolve({age: 21}), Promise.resolve({age: 34})],
toAsync,
uniqBy(a => a.age),
toArray,
); // [{age: 21}, {age: 23}, {age: 34}]