Skip to main content

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
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 findSingle
findSingle<T>(
array: Iterable<T>,
predicate: (el: T) => boolean
): T | undefined

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

Examples

Example 1

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

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

Type Parameters

Parameters

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

Return Type

T | undefined

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

Import directly with a jsr specifier

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