The find method works just like the filter method, but instead of returning an array, it returns the first element for which the iteration function returns a true value. Here’s an example:
var users = [
{ id: 1, admin: false },
{ id: 2, admin: false },
{ id: 3, admin: true }
];
var admin = users.find(function(user){
return user.admin;
});
console.log(admin);
Here’s another example using the arrow function notation:
var accounts = [
{ balance: -10 },
{ balance: 12 },
{ balance: 0 }
];
var account = accounts.find((account)=>{return account.balance === 12});
console.log(account);
Here’s an example that includes a function with a criteria object. In this case Object.keys(criteria)[0] returns the name of the first property of the criteria object.
function findWhere(array, criteria) {
var property = Object.keys(criteria)[0];
return array.find(function(item){
return item[property] === criteria[property];
});
}
var ladders = [
{ id: 1, height: 20 },
{ id: 3, height: 25 }
];
var ladder = findWhere(ladders, { height: 25 })
console.log(ladder);



