Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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.

share|improve this question
    
that is a nice approach – eltonkamami Jul 25 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. – adeneo Jul 25 at 8:45
2  
Using an actual database would probably be better. – Quentin Jul 25 at 8:45
    
You can also use Array's some method – hindmost Jul 25 at 8:49
    
what is database in your code? – Jamiec Jul 25 at 8:51
up vote 1 down vote accepted

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);
share|improve this answer

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;
share|improve this answer
    
The OP just need to test if any element pass the condition. So some is more appropriate. filter will test each element. – hindmost Jul 25 at 9:01
    
@hindmost - you're reading an old version of my answer. – Jamiec Jul 25 at 9:01
    
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 Jul 25 at 9:07
    
@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? – Jamiec Jul 25 at 9:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.