I was given a task to create a object based array of users. Then create a a function which takes in a parameter and returns another array matching the values compared within the first arr
var users = [
{
firstname : "jetlag",
lastname: "son"
},
{
firstname : "becky",
lastname: "love"
},
{
firstname : "just",
lastname: "me"
}
];
function matchName(name) {
for (var i = 0; i < users.length; i++) {
if(users[i].firstname == name) {
return users[i];
} else {
console.log('user does not exist')
}
}
}
console.log(matchName("jetlag"));
I am able to match a specfic username, but what if I just enter j
into the matchName("j")
, i would like to return two objects.
Any help on this would be great. http://jsfiddle.net/dv9aq0m7/
Thanks.