Skip to content

pickBy() function

返回对象的部分副本,仅包含满足提供的谓词的键。

Signature:

typescript
declare function pickBy<T extends object, F extends AsyncEntryPredicate<T>>(
  f: F,
  obj: T,
): Promise<Partial<T>>;

Example

ts
const obj = { a: 1, b: "2", c: true };
pickBy(([key, value]) => key === "a" || value === true, obj); // { a: 1, c: true }

// asynchronous predicate
await pickBy(async ([key, value]) => key === "a" || value === true, obj); // { a: 1, c: true }

// Using with the `pipe` function
pipe(
  obj,
  pickBy(([key, value]) => key === "a" || value === true),
);

await pipe(
  obj,
  pickBy(async ([key, value]) => key === "a" || value === true),
);

see pipe, pick,

Open Source Code

Released under the Apache-2.0 License.