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 firstNotNullishOf
firstNotNullishOf<T, O>(
array: Iterable<T>,
selector: (item: T) => O | undefined | null
): NonNullable<O> | undefined

Applies the given selector to elements in the given array until a value is produced that is neither null nor undefined and returns that value. Returns undefined if no such value is produced.

Examples

Example 1

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

const tables = [
  { number: 11, order: null },
  { number: 12, order: "Soup" },
  { number: 13, order: "Salad" },
];
const nextOrder = firstNotNullishOf(tables, (it) => it.order);

assertEquals(nextOrder, "Soup");

Type Parameters

Parameters

array: Iterable<T>
selector: (item: T) => O | undefined | null

Return Type

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

Import directly with a jsr specifier

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