How to check If a user name exists

How to check If a user name exists

In this post, I'm going to show you the most efficient way to check for an item value in an Array. In our example, We have an array of users and we want to check if a name exists.

Let us start with the some() method (In my view the most efficient way to check for an item value in an array) and then I will show you 3 other methods you can use to check if a name exists.

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

Learn more about the some() method

Array of user names:

//Array of user names 

const users = [
{id: 1,
 name: "Dave",
 isActive: false,
},
{id: 2,
 name: "Ronan",
 isActive: false,
},
{id: 3,
 name: "Mark",
 isActive: false,
},
{id: 4,
 name: "Zaid",
 isActive: false,
},
];
// one line of code for our function using the some() method. 

const ifUserNameExists = (name, arr) => arr.some((el) => el.name === name )
console.log(ifUserNameExists("Zaid", users))

No. 2, The find() method


//another example using find() method

const ifUserNameExists = (name, arr) => {
const exists = arr.find((element) => element.name === name);
return boolean(exists);
}

console.log(ifUserNameExists("Zaid", users))

The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Learn more about the find() method

No. 3, The findIndex() method.

//example using the findIndex() method

const ifUserNameExists = (name, arr) => {
const index = arr.findIndex((element) => element.name === name);
return index > 0;
}

console.log(ifUserNameExists("Zaid", users))

The findIndex() method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

Learn about the findIndex() method

No. 4, The for() loop, which is the least efficient method to use for our task.

//function

const ifUserNameExists = (name, arr) => {
const exists = false;
for(let i = 0; i < arr.length; i++) {
  if(arr[i].name === name){
  exists = true;
  }
}
return exists;
};
console.log(ifUserNameExists("Zaid", users))

As mentioned above, we have many methods to achieve our result, but the loop method is the least efficient way to do this and here's why

  • We have many higher-order functions, which I mentioned above that make our code easier to read and less complex.

  • There is nothing wrong with using a loop, but it can be difficult to debug and test when working on more complex projects.

Conclusion

We have created a function to check for a user name in an array. Using the listed methods, we demonstrated why a "for" Loop is the least efficient. The higher-order functions like some(), find() and findIndex() makes our code less complex and more maintainable.

Please feel free to comment/share this post

Happy coding :-)

Author — David Gillick, Front-End Developer.