findBy (source code)
- Curried: true
- Failsafe status: alternative available
The findBy
function locates the first item in the array that matches the
provided pattern.
pattern
: The pattern using which an object will be matched.entityArray
: The array of objects in which the object with the given pattern will be found.
const array = [
{ id: 1, name: "Sam", address: { street: "First street", pin: 123456 } },
{ id: 2, name: "Oliver", address: { street: "Second street", pin: 654321 } },
];
findBy({ name: "Sam" }, array); // returns object corresponding to Sam
findBy({ id: 2, address: { pin: 654321 } }, array); // returns object corresponding to Oliver
findBy({ id: 3 }, array); // returns undefined