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

x/fathym_common/src/src.deps.ts>maxWith

The Fathym Reference Architecture provides the common foundation for applications built in Typescript.
Go to Latest
function maxWith
import { maxWith } from "https://deno.land/x/fathym_common@v0.0.155/src/src.deps.ts";

Returns the first element having the largest value according to the provided comparator or undefined if there are no elements.

The comparator is expected to work exactly like one passed to Array.sort, which means that comparator(a, b) should return a negative number if a < b, a positive number if a > b and 0 if a === b.

Examples

Example 1

import { maxWith } from "https://deno.land/std@0.224.0/collections/max_with.ts";
import { assertEquals } from "https://deno.land/std@0.224.0/assert/assert_equals.ts";

const people = ["Kim", "Anna", "John", "Arthur"];
const largestName = maxWith(people, (a, b) => a.length - b.length);

assertEquals(largestName, "Arthur");

Parameters

array: Iterable<T>
comparator: (a: T, b: T) => number

Returns

T | undefined