Skip to content

select

Filter and map an array

146 bytes

Usage

Applies a filter and a map operation at once and in one pass. If the filter is omitted, returns all non-nullish mapped values.

import * as
import _
_
from 'radashi'
const
const fish: {
name: string;
weight: number;
source: string;
}[]
fish
= [
{
name: string
name
: 'Marlin',
weight: number
weight
: 105,
source: string
source
: 'ocean',
},
{
name: string
name
: 'Bass',
weight: number
weight
: 8,
source: string
source
: 'lake',
},
{
name: string
name
: 'Trout',
weight: number
weight
: 13,
source: string
source
: 'lake',
},
]
import _
_
.
select<{
name: string;
weight: number;
source: string;
}, number>(array: readonly {
name: string;
weight: number;
source: string;
}[], mapper: (item: {
name: string;
weight: number;
source: string;
}, index: number) => number, condition: ((item: {
name: string;
weight: number;
source: string;
}, index: number) => boolean) | ... 1 more ... | undefined): number[] (+1 overload)
export select

Select performs a filter and a mapper inside of a reduce, only iterating the list one time. If condition is omitted, will select all mapped values that are non-nullish.

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

@example

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

@version12.1.0

select
(
const fish: {
name: string;
weight: number;
source: string;
}[]
fish
,
f: {
name: string;
weight: number;
source: string;
}
f
=>
f: {
name: string;
weight: number;
source: string;
}
f
.
weight: number
weight
,
f: {
name: string;
weight: number;
source: string;
}
f
=>
f: {
name: string;
weight: number;
source: string;
}
f
.
source: string
source
=== 'lake',
) // => [8, 13]