dropWhile

import { dropWhile } from "jsr:@ende/collections";

function

function dropWhile<T>(iterable: Iterable<T>, predicate: (el: T, index: number) => boolean): T[]

Returns an array that drops all elements in the given iterable until the first element that does not match the given predicate.

iterable: Iterable<T>

The iterable to drop elements from.

predicate: (el: T, index: number) => boolean

The function to test each element for a condition. The function receives the element and its index.

Returns

An array that drops all elements until the first element that does not match the given predicate.

Example: Basic usage

import { dropWhile } from "@ende/collections/drop-while";
import { assertEquals } from "@ende/assert";

const numbers = [3, 2, 5, 2, 5];
const dropWhileNumbers = dropWhile(numbers, (number) => number !== 2);

assertEquals(dropWhileNumbers, [2, 5, 2, 5]);

Example: Using the index parameter

import { dropWhile } from "@ende/collections/drop-while";
import { assertEquals } from "@ende/assert";

const array = [20, 30, 20];
const result = dropWhile(array, (_, index) => index < 1);

assertEquals(result, [30, 20]);

Source