Skip to main content
The Deno 2 Release Candidate is here
Learn more
Module

std/collections/mod.ts>findSingle

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

Returns an element if and only if that element is the only one matching the given condition. Returns undefined otherwise.

Example:

import { findSingle } 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 bookings = [
    { month: 'January', active: false },
    { month: 'March', active: false },
    { month: 'June', active: true },
];
const activeBooking = findSingle(bookings, (it) => it.active);
const inactiveBooking = findSingle(bookings, (it) => !it.active);

assertEquals(activeBooking, { month: "June", active: true });
assertEquals(inactiveBooking, undefined); // there are two applicable items

Parameters

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

Returns

T | undefined