Skip to main content

latest
Works with
It is unknown whether this package works with Node.js, Deno, Browsers, Cloudflare Workers, Bun
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Downloads1/wk
Published2 years ago (0.215.0)
function takeWhile
takeWhile<T>(
array: readonly T[],
predicate: (el: T) => boolean
): T[]

Returns all elements in the given collection until the first element that does not match the given predicate.

Examples

Example 1

import { takeWhile } from "@std/collections/take_while";
import { assertEquals } from "@std/assert/assert_equals";

const arr = [1, 2, 3, 4, 5, 6];

assertEquals(
  takeWhile(arr, (i) => i !== 4),
  [1, 2, 3],
);

Type Parameters

Parameters

array: readonly T[]
predicate: (el: T) => boolean

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/collections

Import symbol

import { takeWhile } from "@std/collections";
or

Import directly with a jsr specifier

import { takeWhile } from "jsr:@std/collections";