Function: find()
Call Signature
find<
K,V,IncludeKey,Return>(data,predicate):Return
Defined in: packages/core/src/iterable/find.ts:27
Finds a first item using predicate in an iterable list (Array, Map or Set).
Type Parameters
K
K
V
V extends Record<string, unknown>
IncludeKey
IncludeKey extends boolean = false
Return
Return = IncludeKey extends true ? [K, V] : V | undefined
Parameters
data
IterableList<K, V>
predicate
(item, key, data) => boolean
Returns
Return
first item matched or undefined if not found
Example
Find item using predicate callback
import { find } from '@superutils/core'
const map = new Map<number, { name: string; age: number }>([
[1, { name: 'Alice', age: 30 }],
[2, { name: 'Bob', age: 25 }],
[3, { name: 'Charlie', age: 35 }],
])
const result = find(map, ({ name }) => name === 'Bob')
console.log({ result })
// result: { name: 'Bob', age: 25 }Call Signature
find<
K,V,IncludeKey,Return>(data,options):Return
Defined in: packages/core/src/iterable/find.ts:60
Find the first item in an iterable list (Array, Map or Set) using search() function
Type Parameters
K
K
V
V extends Record<string, unknown>
IncludeKey
IncludeKey extends boolean = false
Return
Return = IncludeKey extends true ? [K, V] : V | undefined
Parameters
data
IterableList<K, V>
items to search
options
FindOptions<K, V, IncludeKey>
filter options. See FindOptions
Returns
Return
Example
Find item using search options
import { find } from '@superutils/core'
const map = new Map<number, { name: string; age: number }>([
[1, { name: 'Alice', age: 30 }],
[2, { name: 'Bob', age: 25 }],
[3, { name: 'Charlie', age: 35 }],
])
const result = find(map, {
query: { name: 'Bob' }
})
console.log({ result })
// result: { name: 'Bob', age: 25 }