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 partition
partition<T>(
array: Iterable<T>,
predicate: (el: T) => boolean
): [T[], T[]]

Returns a tuple of two arrays with the first one containing all elements in the given array that match the given predicate and the second one containing all that do not.

Examples

Example 1

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

const numbers = [5, 6, 7, 8, 9];
const [even, odd] = partition(numbers, (it) => it % 2 === 0);

assertEquals(even, [6, 8]);
assertEquals(odd, [5, 7, 9]);

Type Parameters

Parameters

array: Iterable<T>
predicate: (el: T) => boolean

Return Type

[T[], T[]]
partition<T, U extends T>(
array: Iterable<T>,
predicate: (el: T) => el is U
): [U[], Exclude<T, U>[]]

Returns a tuple of two arrays with the first one containing all elements in the given array that match the given predicate and the second one containing all that do not.

Examples

Example 1

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

const numbers = [5, 6, 7, 8, 9];
const [even, odd] = partition(numbers, (it) => it % 2 === 0);

assertEquals(even, [6, 8]);
assertEquals(odd, [5, 7, 9]);

Type Parameters

Parameters

array: Iterable<T>
predicate: (el: T) => el is U

Return Type

[U[], Exclude<T, U>[]]

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 { partition } from "@std/collections";
or

Import directly with a jsr specifier

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