The find method in JavaScript can be useful when you need to search an array and return a single found element.
Example Data
Here's some simple data that we'll use in the examples below. It's just a small array of objects:
const employees = [
{
name: "Jacob",
department: "sales"
},
{
name: "Erica",
department: "HR"
},
{
name: "Erica",
department: "sales"
},
{
name: "Mia",
department: "catering"
}
]
How to Use
The find
method will iterate through each element in an array, using each element in a "search" function that we define. It returns only the first element it finds, and returns undefined
if nothing is found.
Let's say we need to search this data for an element having the name Erica
and works in sales
:
const found = employees.find((el => el.name === "Erica" && el.department === "sales");
or, you could do the same thing in a more verbose form:
const found = employees.find(el => {
return el.name === `Erica` && el.department === "sales";
});
You can also use a separate search function, like this:
const isErica = (name) => {
return name === `Erica`;
}
const found = employees.find(isErica);
Result:
After executing either of the above, our found
variable would contain:
{
name: "Erica",
department: "HR"
}
That's it! Hope that helps someone! 😎