0

What is the best method to handle this? consider i'm handling a sign-up list database and i want to know whether a user the same username already exists or not?

something like this:

for (var i = 0; i < database.user.length; ++i) {
    if( database.user[i].username === username ) {
        return true;
    }
}
return false;

is there something better??

PS1: database.user is an array of objects

PS2: and I already know about the Array->indexOf func and it didn't help.

6
  • that is a nice approach Commented Jul 25, 2016 at 8:45
  • Yours seems fine, and is probably the fastest. However, most databases have a way to check if something "exists" without getting the result, if that's an option. Commented Jul 25, 2016 at 8:45
  • 2
    Using an actual database would probably be better. Commented Jul 25, 2016 at 8:45
  • You can also use Array's some method Commented Jul 25, 2016 at 8:49
  • what is database in your code? Commented Jul 25, 2016 at 8:51

2 Answers 2

1

Your code goes all the way the whole array even if an item at index 0 matches your query. You could use Array.prototype.some but even that one will fall short in performance compared to Array.prototype.findIndex. So my advice would be using findIndex.

database.user.findIndex(e => e.username === username) === -1 && login(username);
Sign up to request clarification or add additional context in comments.

Comments

1

In javascript, any other way of doing this is just hiding the fact that, under the hood, they are just doing exactly what your code already does.

For example, you could filter your array, and check whether it's length is >= 1

var userExists = database.user.filter(u => u.username === username).length >= 1;

It's shorter, and arguably a little more readable than your original, but its not necessarily best, and neither is it likely to be faster.

Slightly better would be to use find - as this returns as soon as an element matches, meaning the whole array is not evaluated

var userExists = database.user.find(u => u.username === username) !== undefined;

(some would also be appropriate)


Note, this answer uses ES6 format for lambda expressions, the equivalent in unsupporting browsers would be

var userExists = database.user.filter(function(u) {return  u.username == username;}).length >= 1
// or
var userExists = database.user.find(function(u) {return  u.username == username;}) !== undefined;

3 Comments

The OP just need to test if any element pass the condition. So some is more appropriate. filter will test each element.
The OP doesn't need return the found value, he already have it (the searching username). So find does redundant work. Furthermore it require ES6.
@hindmost now I have no idea what you're talking about! My point was only that instead of a for loop, there are any number of ways to do the same thing. Furthermore my answer already says what you just said about ES6 and provides an alternative! Did you have a point?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.