Array.Find Shorthand JavaScript
// Array.find Shorthand javascript
// Longhand:
const pets = [
{ type: 'Dog', name: 'Max'},
{ type: 'Cat', name: 'Karl'},
{ type: 'Dog', name: 'Tommy'},
]
function findDog(name) {
for(let i = 0; i<pets.length; i++) {
if(pets[i].type === 'Dog' && pets[i].name === name) {
return pets[i];
}
}
}
console.log(findDog("Tommy")); // { type: 'Dog', name: 'Tommy' }
// Shorthand:
const findDog_ = pets.find(pet => pet.type ==='Dog' && pet.name === 'Tommy');
console.log(findDog_); // { type: 'Dog', name: 'Tommy' }
Chetan Nada