I'm trying to iterate through the elements of this array and return 'gotcha, bill' when the 'for in' loop finds the value 'bill'. However, all I get is 'not him' repeated four times. I've gone through my code multiple times but I can't figure out what I'm missing. This is my first time using a for in loop so I might not fully understand how it works.
Any assistance would be greatly appreciated!
var names = [{name: 'steven', age: 22}, {name: 'bill', age: 13}];
function findBill(array) {
for(let i = 0; i < array.length; i++) {
for(let key in array[i]) {
if(key === 'bill') {
return console.log('gotcha, bill');
} else {
console.log('not him');
}
}
}
}
findBill(names)