Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/collections/mod.ts>partition

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
function partition
import { partition } from "https://deno.land/std@0.139.0/collections/mod.ts";

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

Example:

import { partition } from "https://deno.land/std@0.139.0/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@0.139.0/testing/asserts.ts";

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 ])

Parameters

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

Returns

[T[], T[]]