I am trying to understand the fundamentals of some js concepts, specifically on why my function returns on defined value
accountName is just a simple string, like "facebook" the return value of accounts is just an array
Original Function
function getAccount(accountName) {
var accounts = Storage.getItemSync('accounts');
var matchedAccount
for(account in accounts){
if (account.name === accountName){
matchedAccount = accountName;
}
}
return matchedAccount;
}
Working Version
function getAccount(accountName) {
var accounts = Storage.getItemSync('accounts');
var matchedAccount
accounts.forEach(function(account) {
if(account.name === accountName){
matchedAccount = account
}
});
return matchedAccount;
}
The original function returns Undefined, while the working version returns the correct results. Why is this happening? Arn't I iterating over my array all the same?