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

x/ayonli_jsext/array/index.ts>partition

A JavaScript extension package for building strong and modern applications.
Latest
function partition
import { partition } from "https://deno.land/x/ayonli_jsext@v0.9.72/array/index.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.

Examples

Example 1

import { partition } from "@ayonli/jsext/array";

const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const [even, odd] = partition(arr, item => item % 2 === 0);

console.log(even); // [0, 2, 4, 6, 8]
console.log(odd); // [1, 3, 5, 7, 9]

Parameters

arr: T[]
predicate: (item: T, i: number) => boolean

Returns

[T[], T[]]