On Fri, Mar 9, 2012 at 2:30 AM, Angelo Chen <[email protected]> wrote:
> Hi,
>
> Struggling to change coding practice, following has two samples, which
> one is correct? or any other approaches available?
For this particular use case, it would be more convenient if 'users'
was an object instead of an array, since you could then simply
reference users[id] instead of having to walk the array. In fact, even
if you do need an array for other use cases, it may be worth
considering keeping an object as well, especially if the number of
users gets long and / or get_user is called a lot.
--
Martin Cooper
> exports.get_user = function (id, func) {
> var found = false
> for (var i = 0; i < users.length; i ++) {
> if (users[i].id === id) {
> found = true
> func(null, users[i])
> break;
> }
> }
> if (!found)
> func("NOT_FOUND", null)
> }
>
>
> exports.get_user = function (id, func) {
> for (var i = 0; i < users.length; i ++) {
> if (users[i].id === id) {
> func(null, users[i])
> break;
> }
> if (i == users.length)
> func("NOT_FOUND", null)
> }
> }
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en