Skip to main content

findKey()

findKey<T>(object, predicate): string | undefined

Returns the key of the first element predicate returns truthy for.


Type Parametersโ€‹

T: T extends Record<string, unknown>โ€‹

The type of the object.


Parametersโ€‹

object: Tโ€‹

The object to inspect.

predicate: (value, key, object) => booleanโ€‹

The function invoked per iteration.


Returns: string | undefinedโ€‹

The key of the matched element, or undefined if not found.


Sinceโ€‹

2.0.0


Performanceโ€‹

O(n) where n is the number of own enumerable properties.


Also known asโ€‹

findFirst (Effect) ยท findKey (Lodash, es-toolkit) ยท โŒ (Remeda, Radashi, Ramda, Modern Dash, Antfu)


Exampleโ€‹

const users = {
barney: { age: 36, active: true },
fred: { age: 40, active: false },
pebbles: { age: 1, active: true }
};

findKey(users, u => u.age < 40);
// => 'barney' (iteration order is not guaranteed)

findKey(users, u => u.active === false);
// => 'fred'

findKey(users, u => u.age > 100);
// => undefined

How it works?โ€‹

Returns the key of the first element that satisfies a predicate.

Search Processโ€‹

Not Foundโ€‹


Use Casesโ€‹

Find User by Property ๐Ÿ“Œโ€‹

Find object key based on a value condition. Essential for reverse lookups in object maps.

import { findKey } from "pithos/arkhe/object/find-key";

const users = {
user_1: { name: "Alice", active: true },
user_2: { name: "Bob", active: false },
user_3: { name: "Charlie", active: true },
};

// Find first active user's key
const activeUserKey = findKey(users, (user) => user.active);
console.log(activeUserKey); // "user_1"

// Find user by name
const bobKey = findKey(users, (user) => user.name === "Bob");
console.log(bobKey); // "user_2"

Reverse Enum Lookup ๐Ÿ“Œโ€‹

Find enum key from its value. Critical for translating values back to identifiers.

import { findKey } from "pithos/arkhe/object/find-key";

const HttpStatus = {
OK: 200,
CREATED: 201,
BAD_REQUEST: 400,
NOT_FOUND: 404,
INTERNAL_ERROR: 500,
} as const;

function getStatusName(code: number): string {
const name = findKey(HttpStatus, (value) => value === code);
return name ?? "UNKNOWN";
}

console.log(getStatusName(200)); // "OK"
console.log(getStatusName(404)); // "NOT_FOUND"
console.log(getStatusName(999)); // "UNKNOWN"

Configuration Validation finding invalid entriesโ€‹

Find the first invalid configuration entry. Important for debugging configuration issues.

import { findKey } from "pithos/arkhe/object/find-key";

interface ConfigValue {
value: unknown;
required: boolean;
}

const config: Record<string, ConfigValue> = {
apiKey: { value: "abc123", required: true },
timeout: { value: null, required: true },
debug: { value: false, required: false },
endpoint: { value: "", required: true },
};

// Find first missing required config
const missingKey = findKey(
config,
(entry) => entry.required && (entry.value === null || entry.value === "")
);

if (missingKey) {
throw new Error(`Missing required configuration: ${missingKey}`);
}
// Throws: "Missing required configuration: timeout"