Skip to content

searchObjects

searchObjects<T, S, C>(query, items, options): ObjectSearchResult<T>[]

Perform fuzzy search across object arrays with weighted keys.

Wraps searchKeys() with an ergonomic API that accepts row-oriented objects and returns matched items directly. Key names written as string literals are type-checked against the item type (see KeyPath).

Type Parameter Default type
T -
S extends string string
C extends string string
Parameter Type
query string
items readonly T[]
options CheckedOptions<T, S, C, KeySearchOptions>

ObjectSearchResult<T>[]

const users = [
{ name: 'John Smith', email: 'john@example.com' },
{ name: 'Jane Doe', email: 'jane@example.com' },
];
const results = searchObjects('john', users, {
keys: [{ name: 'name', weight: 2.0 }, 'email'],
});
// results[0].item → { name: 'John Smith', email: 'john@example.com' }
// Rank an exact match on any one key first: 'matched' averages over the
// keys that match only (see KeySearchOptions.scoreMode).
searchObjects('smith', users, { keys: ['name', 'email'], scoreMode: 'matched' });
// Let the terms of the query match different keys (see
// KeySearchOptions.matchMode): 'smith example' finds John Smith, whose
// name and email each contain one of the terms.
searchObjects('smith example', users, { keys: ['name', 'email'], matchMode: 'crossKey' });