findLastKey
object
This method is like findKey except that it iterates over elements of a collection in the opposite order.
Installation
Import
import { findLastKey } from '@tulx/utils';Source Code
Implementation
/**
* This method is like findKey except that it iterates over elements of a collection in the opposite order.
*
* @param object - The object to inspect.
* @param predicate - The function invoked per iteration.
* @returns Returns the key of the matched element, else undefined.
*
* @example
* ```ts
* const users = {
* 'barney': { 'age': 36, 'active': true },
* 'fred': { 'age': 40, 'active': false },
* 'pebbles': { 'age': 1, 'active': true }
* };
* findLastKey(users, (o) => o.age < 40); // 'pebbles'
* ```
*/
export function findLastKey<T>(
object: Record<string, T>,
predicate: (value: T, key: string, object: Record<string, T>) => boolean
): string | undefined {
const keys = Object.keys(object);
for (let i = keys.length - 1; i >= 0; i--) {
const key = keys[i];
if (predicate(object[key], key, object)) {
return key;
}
}
return undefined;
}
Example
import { findLastKey } from '@tulx/utils';
const users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
findLastKey(users, (o) => o.age < 40); // 'pebbles'Related Functions
assign
Assigns own enumerable string keyed properties of source objects to the destination object.
assignIn
This method is like assign except that it iterates over own and inherited source properties.
assignInWith
This method is like assignIn except that it accepts customizer which is invoked to produce the assigned values.
assignWith
This method is like assign except that it accepts customizer which is invoked to produce the assigned values.
at
Creates an array of values corresponding to paths of object.
create
Creates an object that inherits from the prototype object.