Skip to content

selectFirst

Find and map the first array element meeting a condition

143 bytes

Usage

The selectFirst function combines the functionality of find and map operations on an array. It iterates through the array, applying a mapper function to each element, and returns the first mapped value that satisfies a given condition. If no condition is provided, it returns the first non-nullish mapped value.

This function is particularly useful when you need to find and transform an element in a single operation, potentially saving time and improving code readability.

Key features:

  • Short-circuits on the first element that satisfies the condition
  • Allows for separate mapping and condition functions
  • Returns undefined if no element satisfies the condition or if the array is empty/nullish
import * as _ from 'radashi'
// Find the first even number and double it
_.selectFirst(
[1, 3, 4, 6, 8],
x => x * 2,
x => x % 2 === 0,
)
// => 8
// Find the first non-empty string and convert to uppercase
_.selectFirst(
['', null, 'hello', 'world'],
s => s?.toUpperCase(),
s => s !== null && s !== '',
)
// => 'HELLO'
// Find the first object with a specific property and extract a value
const users = [
{ id: 1, name: 'Alice', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Charlie', age: 35 },
]
_.selectFirst(
users,
user => user.name,
user => user.age > 30,
)
// => 'Charlie'
// Using default condition (non-nullish)
_.selectFirst([null, undefined, 0, '', false, 'found'], x => x)
// => 0