Back to Es Toolkit

dropRightWhile (函数式编程)

docs/zh_hans/fp/reference/dropRightWhile.md

1.49.01009 B
Original Source

dropRightWhile (函数式编程)

创建一个在谓词通过时丢弃末尾值的函数。与函数式编程的 pipe 一起使用。

typescript
const result = pipe(array, dropRightWhile(predicate));

::: info

在不需要管道组合的普通代码中,建议使用原始 es-toolkit 的 dropRightWhile。当你要用 pipe 串联转换时,请使用这个 fp 版本。

:::

用法

dropRightWhile 会从管道中数组的末尾开始遍历,并在 predicate 返回 true 时移除值。遇到第一个不满足条件的值时停止。

typescript
import { dropRightWhile, pipe } from 'es-toolkit/fp';

pipe(
  [1, 2, 3, 4],
  dropRightWhile(value => value > 2)
); // => [1, 2]

参数

  • predicate ((item: T) => boolean): 判断是否应移除末尾值的函数。

返回值

((array: readonly T[]) => T[]): 一个将 readonly T[] 映射为从末尾移除值后剩余数组的函数。