Skip to main content

pluck

pluck() function

Returns Iterable/AsyncIterable by plucking the same named property off all objects in Iterable/AsyncIterable supplied.

Signature:

declare function pluck<O extends object, K extends keyof O>(key: K, iterable: Iterable<O>): IterableIterator<O[K]>;

Returns:

IterableIterator<O[K]>

Example

const iter = pluck('age', [{age:21}, {age:22}, {age:23}]);
iter.next(); // {done:false, value: 21}
iter.next(); // {done:false, value: 22}
iter.next(); // {done:false, value: 23}

// with pipe
pipe(
[{age:21}, {age:22}, {age:23}],
pluck('age'),
toArray,
); // [21, 22 ,23]

// if you want to use asynchronous callback
await pipe(
Promise.resolve([{age:21}, {age:22}, {age:23}]),
toAsync,
pluck('age'),
toArray,
); // [21, 22 ,23]

// with toAsync
await pipe(
[Promise.resolve({age:21}), Promise.resolve({age:22}), Promise.resolve({age:23})],
toAsync,
pluck('age'),
toArray,
); // [21, 22 ,23]

see pipe, toAsync, toArray