js-helpers

Find

Find(Array array, [Function, Object] predicate)

Finds the first item from *Array* array that returns true from *[Function, Object]* predicate.

Arguments

Array array

For array - Collection of items to iterate over with *[Function, Object]* predicate.

[[Function, Object] predicate=undefined]

For Function: *Function* to invoke per item of *Array* array to determine if an item has been found. *Function* predicate should return *Boolean* true || false.

Use

import { Find } from "@taystack/js-helpers";


const items = [{a: 1, b: 1}, {a: 2, b: 1}, {a: 3, b: 1}];

// Function predicate

const found = Find(items, (item) => (item.a === 2));

// Shorthand Object predicate

const shorthand = Find(items, {a: 2});

Source: @taystack/js-helpers hosted on GitHub Author: taystack