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
import _
_
from 'radashi'
// Find the first even number and double it
import _
_
.
selectFirst<number, number>(array: readonly number[], mapper: (item: number, index: number) => number, condition?: ((item: number, index: number) => boolean) | undefined): number | undefined
export selectFirst

Select performs a find + map operation, short-circuiting on the first element that satisfies the prescribed condition. If condition is omitted, will select the first mapped value which is non-nullish.

@seehttps://radashi.js.org/reference/array/selectFirst

@example

selectFirst(
[1, 2, 3, 4],
x => x * x,
x => x > 2
)
// => 9

@version12.2.0

selectFirst
(
[1, 3, 4, 6, 8],
x: number
x
=>
x: number
x
* 2,
x: number
x
=>
x: number
x
% 2 === 0,
)
// => 8
// Find the first non-empty string and convert to uppercase
import _
_
.
selectFirst<string | null, string | undefined>(array: readonly (string | null)[], mapper: (item: string | null, index: number) => string | undefined, condition?: ((item: string | null, index: number) => boolean) | undefined): string | undefined
export selectFirst

Select performs a find + map operation, short-circuiting on the first element that satisfies the prescribed condition. If condition is omitted, will select the first mapped value which is non-nullish.

@seehttps://radashi.js.org/reference/array/selectFirst

@example

selectFirst(
[1, 2, 3, 4],
x => x * x,
x => x > 2
)
// => 9

@version12.2.0

selectFirst
(
['', null, 'hello', 'world'],
s: string | null
s
=>
s: string | null
s
?.
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
(),
s: string | null
s
=>
s: string | null
s
!== null &&
s: string
s
!== '',
)
// => 'HELLO'
// Find the first object with a specific property and extract a value
const
const users: {
id: number;
name: string;
age: number;
}[]
users
= [
{
id: number
id
: 1,
name: string
name
: 'Alice',
age: number
age
: 30 },
{
id: number
id
: 2,
name: string
name
: 'Bob',
age: number
age
: 25 },
{
id: number
id
: 3,
name: string
name
: 'Charlie',
age: number
age
: 35 },
]
import _
_
.
selectFirst<{
id: number;
name: string;
age: number;
}, string>(array: readonly {
id: number;
name: string;
age: number;
}[], mapper: (item: {
id: number;
name: string;
age: number;
}, index: number) => string, condition?: ((item: {
id: number;
name: string;
age: number;
}, index: number) => boolean) | undefined): string | undefined
export selectFirst

Select performs a find + map operation, short-circuiting on the first element that satisfies the prescribed condition. If condition is omitted, will select the first mapped value which is non-nullish.

@seehttps://radashi.js.org/reference/array/selectFirst

@example

selectFirst(
[1, 2, 3, 4],
x => x * x,
x => x > 2
)
// => 9

@version12.2.0

selectFirst
(
const users: {
id: number;
name: string;
age: number;
}[]
users
,
user: {
id: number;
name: string;
age: number;
}
user
=>
user: {
id: number;
name: string;
age: number;
}
user
.
name: string
name
,
user: {
id: number;
name: string;
age: number;
}
user
=>
user: {
id: number;
name: string;
age: number;
}
user
.
age: number
age
> 30,
)
// => 'Charlie'
// Using default condition (non-nullish)
import _
_
.
selectFirst<string | number | boolean | null | undefined, string | number | boolean | null | undefined>(array: readonly (string | number | boolean | null | undefined)[], mapper: (item: string | number | boolean | null | undefined, index: number) => string | ... 3 more ... | undefined, condition?: ((item: string | ... 3 more ... | undefined, index: number) => boolean) | undefined): string | ... 3 more ... | undefined
export selectFirst

Select performs a find + map operation, short-circuiting on the first element that satisfies the prescribed condition. If condition is omitted, will select the first mapped value which is non-nullish.

@seehttps://radashi.js.org/reference/array/selectFirst

@example

selectFirst(
[1, 2, 3, 4],
x => x * x,
x => x > 2
)
// => 9

@version12.2.0

selectFirst
([null,
var undefined
undefined
, 0, '', false, 'found'],
x: string | number | boolean | null | undefined
x
=>
x: string | number | boolean | null | undefined
x
)
// => 0