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