2

This is basically a programming style question in javascript.

Sometimes when I'm coding I find myself looping through a collection of data, like for example, a collection of users:

var users = [
    {
        id: 'USER-435',
        name: 'James',
        email: '[email protected]'
    },{
        id: 'USER-7897',
        name: 'Mark',
        email: '[email protected]'
    },{
        id: 'USER-2345',
        name: 'Harry',
        email: '[email protected]'
    }
] 

Important: This data comes from the server and it needs to be in order.

If I want to get the properties of a specific user by its ID, I will have to loop through the array to find it.

So what I have done is to loop through the array once and create properties in the array with the user's ID as its key. This way I can access each user using its ID without looping through the array. Because the = operator creates a reference to the object and not a copy, each property added to the array will be a reference.

The only problem I have found is that if the ID of the user is a number it will be part of the array. So, if we have a collection with 3 users, with one of their ID been 120, that will set the length of the array to 121. A quick could be add the property as 'id-120', but it doesn't feel very clean.

I can also create a separate object for the collection instead of creating new properties in the array, but that will create a new object I need to take care of.

I just wanted to ask what people think about this type of pattern and if you have a better way of doing it.

0

2 Answers 2

2

You only have a few options to accomplish this. It's a pretty common pattern to re-map an array to an object to do lookups by key.

I would not worry too much about creating a 'users_by_id' object, it is unlikely to cause you much trouble (unless your code is already very complex)

But, I have a suggestion for your particular problem. Why not create a property on the array called 'by_id' and make that the object that you index by id.

users.by_id['USER-2345'];

It solves the 'extra variable' problem, the key-clobbering problem and the code becomes self-explanatory to boot.

Good luck.

PS - also check out underscore.js or lodash - they have routines that make this kind of thing very easy. They'll save you a ton of time.

Sign up to request clarification or add additional context in comments.

Comments

0

I would use Array.filter here. Something like:

users.filter( function (v) { return v.id === 'USER-435'; } );

In a function:

function findUser(id) {
  var user = users.filter( function (v) { return v.id === id; } );
  return user.length ? user[0] : {id: 'not found'};
}

This will need looping, but until your array contains, say, more than a million elements, I wouldn't worry about that. To quote Tony Hoare:

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

See also MDN
And (to be fair) also THE FALLACY OF PREMATURE OPTIMIZATION

2 Comments

The main question is how to avoid looping through the array
Sorry but @JayKuri 's answer it's much cleaner

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.