The find method from Lodash iterates over elements of a collection, returning the first element that a predicate function returns truthy for. It supports multiple predicate formats including functions, objects, arrays, and strings, providing flexible and powerful search capabilities for both arrays and objects.
Note: This Knowledge Tile provides comprehensive documentation specifically for Lodash's
findmethod. It is a focused, single-method tile rather than complete Lodash package documentation.
npm install lodashimport _ from "lodash";
// Use as _.find()For CommonJS:
const _ = require("lodash");
// Use as _.find()With modern bundlers and tree-shaking:
// Option 1: Use lodash-es for better tree-shaking
import { find } from "lodash-es";
// Option 2: Direct import from specific module (if available)
import find from "lodash/find";
// Note: Tree-shaking with lodash requires lodash-es or modular importsimport _ from "lodash";
const users = [
{ user: 'barney', age: 36, active: true },
{ user: 'fred', age: 40, active: false },
{ user: 'pebbles', age: 1, active: true }
];
// Find using a function predicate
const youngUser = _.find(users, function(o) { return o.age < 40; });
// => { user: 'barney', age: 36, active: true }
// Find using object properties
const child = _.find(users, { age: 1, active: true });
// => { user: 'pebbles', age: 1, active: true }
// Find using property-value pair
const inactiveUser = _.find(users, ['active', false]);
// => { user: 'fred', age: 40, active: false }
// Find using property name
const activeUser = _.find(users, 'active');
// => { user: 'barney', age: 36, active: true }The find method is part of Lodash's collection iteration system with several key architectural features:
find can terminate early without processing the entire collection, providing significant performance benefitsfind iterates, not to the entire collection upfront_.iteratee system for predicate normalization, allowing flexible predicate formatsIterates over elements of collection, returning the first element predicate returns truthy for.
/**
* Iterates over elements of collection, returning the first element
* predicate returns truthy for. The predicate is invoked with three arguments:
* (value, index|key, collection).
*
* @param {Array|Object} collection - The collection to search
* @param {Function|Object|Array|string} [predicate=_.identity] - The function invoked per iteration.
* - Function: Receives (value, index|key, collection) and returns boolean
* - Object: Uses _.matches for deep property matching
* - Array: Uses _.matchesProperty for [property, value] pair matching
* - String: Uses _.property for truthy property checking
* @returns {*} Returns the matched element, else undefined
*/
function find(collection, predicate);Parameters:
collection (Array|Object): The collection to searchpredicate (Function|Object|Array|string) [optional, defaults to _.identity]: The function invoked per iterationPredicate Format Options:
Function: function(value, index|key, collection) => boolean
Object: { property: value, ... }
===) for value comparisonArray: [property, value]
String: "property"
function(o) { return !!o[property]; }Returns:
*: The first matched element, or undefined if no match is foundBehavior:
undefined if no element matches the predicateUsage Examples:
import _ from "lodash";
// Array of objects
const employees = [
{ name: 'Alice', department: 'Engineering', salary: 75000 },
{ name: 'Bob', department: 'Sales', salary: 60000 },
{ name: 'Charlie', department: 'Engineering', salary: 80000 }
];
// Function predicate with custom logic
const highEarner = _.find(employees, function(employee) {
return employee.salary > 70000;
});
// => { name: 'Alice', department: 'Engineering', salary: 75000 }
// Object predicate for exact match
const salesPerson = _.find(employees, { department: 'Sales' });
// => { name: 'Bob', department: 'Sales', salary: 60000 }
// Array predicate for property-value pair
const charlie = _.find(employees, ['name', 'Charlie']);
// => { name: 'Charlie', department: 'Engineering', salary: 80000 }
// String predicate for truthy property
const products = [
{ name: 'Laptop', featured: false },
{ name: 'Phone', featured: true },
{ name: 'Tablet' }
];
const featuredProduct = _.find(products, 'featured');
// => { name: 'Phone', featured: true }
// Working with plain arrays
const numbers = [1, 2, 3, 4, 5];
const evenNumber = _.find(numbers, function(n) { return n % 2 === 0; });
// => 2
// Working with objects (not arrays)
const userRoles = {
admin: { permissions: ['read', 'write', 'delete'] },
editor: { permissions: ['read', 'write'] },
viewer: { permissions: ['read'] }
};
const writeUser = _.find(userRoles, function(role) {
return role.permissions.includes('write');
});
// => { permissions: ['read', 'write', 'delete'] }
// No match found
const notFound = _.find(employees, { department: 'Marketing' });
// => undefined
// Chaining with lazy evaluation and shortcut fusion
const largeDataset = _.range(1, 10000);
const result = _(largeDataset)
.map(n => n * n) // Only applied to elements until first match
.filter(n => n > 100) // Only applied to elements until first match
.find(n => n % 7 === 0); // Stops at first match: 121 (11²)
// => 121
// Note: Only processes ~11 elements instead of all 10,000Performance Characteristics:
Integration with Lodash Ecosystem:
_.iteratee internally for predicate processing, enabling the flexible predicate formats_(collection).chain() sequencesError Handling:
undefined for non-collection inputs