docs/reference/compat/util/iteratee.md
::: warning Use direct functions or property access instead
This iteratee function performs slowly due to complex type conversions and handling of various cases.
Instead, use faster and more modern direct functions or property access.
:::
Creates a function that returns a value from an element.
const getter = iteratee(source);
iteratee(value?)Use iteratee when you want to create a function that extracts values from collection elements or checks conditions. It performs different actions based on the type of argument provided.
import { iteratee } from 'es-toolkit/compat';
// Function: Returns the given function as-is
const func = iteratee(object => object.a);
[{ a: 1 }, { a: 2 }, { a: 3 }].map(func);
// Returns: [1, 2, 3]
// Property name: Function that returns the value of that property
const getA = iteratee('a');
[{ a: 1 }, { a: 2 }, { a: 3 }].map(getA);
// Returns: [1, 2, 3]
// Object: Function that checks if it matches the given object
const matchesObj = iteratee({ a: 1 });
[
{ a: 1, b: 2 },
{ a: 2, b: 3 },
{ a: 1, c: 4 },
].find(matchesObj);
// Returns: { a: 1, b: 2 }
// Property-value pair: Function that checks if the property matches a specific value
const matchesProperty = iteratee(['a', 1]);
[{ a: 1 }, { a: 2 }, { a: 3 }].find(matchesProperty);
// Returns: { a: 1 }
// null or no argument: Function that returns the element as-is
const identity = iteratee();
[{ a: 1 }, { a: 2 }, { a: 3 }].map(identity);
// Returns: [{ a: 1 }, { a: 2 }, { a: 3 }]
Actions based on argument type:
value (symbol | number | string | object | null | ((...args: any[]) => unknown), optional): The value to convert to an iteratee. Default is null.((...args: any[]) => any): Returns the new iteratee function.