let teams = [
{ name : 'Patriots' },
{ name : 'Dolphins' },
{ name : 'Jets' },
{ name : 'Bills' }
]
let search = (name) => {
for( let i = 0; i < teams.length; i++ ) {
if( name === teams[i].name ) {
return teams[i];
}
return false;
}
}
console.log(search('Patriots')); // returns { name : 'Patriots }
console.log(search('Dolphins')); // return false
I am trying to write a search function to search an array of objects. When I search for the first object it returns it. When I search for any other object in the array it return false. Is there a way to have the for loop run completely before moving onto my if else statements or is there a better way to do this?