JSFiddle - React, Tailwind, and code Playground
JavaScript
var users = [
{
firstname : "jetlag",
lastname: "son"
},
{
firstname : "becky",
lastname: "love"
},
{
firstname : "just",
lastname: "me"
}
];
function matchName(name) {
var validUsers = []; //the array of users that pass the logic
for (var i = 0; i < users.length; i++) {
if(users[i].firstname.lastIndexOf(name) === 0) { //this is "starts with" logic
validUsers.push(users[i]); //add the element if it's valid
}
}
//I moved the logic of the outside the loop
if (validUsers.length === 0)
console.log('No matching users found')
return validUsers;
}
console.log(matchName("j"));