Documentation
    Preparing search index...

    Function has

    • Checks if at least one element in the array satisfies the given condition.

      Similar to JavaScript's native some() method. Returns true immediately when the first element satisfying the condition is found.

      Processing cost: Native some stops at the first matching element: worst-case N predicate calls and constant auxiliary storage.

      Type Parameters

      • T

        The type of elements in the array

      Parameters

      • elements: readonly T[]

        The readonly array to check

      • pred: (elem: T) => boolean

        The predicate function to test elements

      Returns boolean

      Boolean indicating if any element satisfies the condition

      const numbers = [1, 3, 5, 7, 8, 9];
      const products = [
      { name: "Apple", price: 100, inStock: true },
      { name: "Banana", price: 50, inStock: false },
      { name: "Orange", price: 80, inStock: true },
      ];

      const hasEvenNumber = ArrayUtil.has(numbers, (num) => num % 2 === 0);
      console.log(hasEvenNumber); // true (8 exists)

      const hasExpensiveItem = ArrayUtil.has(
      products,
      (product) => product.price > 90,
      );
      console.log(hasExpensiveItem); // true (Apple costs 100)

      const hasOutOfStock = ArrayUtil.has(
      products,
      (product) => !product.inStock,
      );
      console.log(hasOutOfStock); // true (Banana is out of stock)

      contracts/common.md#principled-implementation The native some implements the existential search and returns at the first satisfying element, and the predicate receives only the element.

      contracts/common.md#clear-and-simple-design A one-expression delegation that keeps the native short-circuit visible.

      contracts/common.md#prohibited-implementation-shortcuts The predicate alone decides membership.

      contracts/common.md#meaningful-documentation The comment states the short-circuit and gives examples.