16

I have an array which is:

[ 4ff023908ed2842c1265d9e4, 4ff0d75c8ed2842c1266099b ]

And I have to find if the following, is inside that array

4ff0d75c8ed2842c1266099b

Here is what I wrote:

Array.prototype.contains = function(k) {
  for(p in this)
     if(this[p] === k)
        return true;
  return false;
}

Apparently, it doesn't work properly, or better sometimes it works, but it looks to me blocking. Is there anyone that can check that one?

many thanks

4
  • 3
    "Blocking"? Also, don't iterate arrays with for in. Anyway, just write it as this.indexOf(k) > -1... Commented Jul 2, 2012 at 1:33
  • Are these ObjectId objects? Commented Jul 2, 2012 at 1:43
  • 1
    What is you definition of blocking? In Node, it usually means "wait for I/O to finish". Commented Jul 2, 2012 at 1:45
  • @Thilo yep I mean that JohnnyHK yes they are, minitech would you like to put that as answer? Commented Jul 2, 2012 at 2:43

1 Answer 1

32

Non-blocking search function

Array.prototype.contains = function(k, callback) {
    var self = this;
    return (function check(i) {
        if (i >= self.length) {
            return callback(false);
        }

        if (self[i] === k) {
            return callback(true);
        }

        return process.nextTick(check.bind(null, i+1));
    }(0));
}

Usage:

[1, 2, 3, 4, 5].contains(3, function(found) {
    if (found) {
        console.log("Found");
    } else {
        console.log("Not found");
    }
});

However, for searching the value in the array it is better to use Javascript built-in array search function, as it will be much faster (so that you probably won't need it to be non-blocking):

if ([1, 2, 3, 4, 5].indexOf(3) >= 0) {
    console.log("Found");
} else {
    console.log("Not found");
}

Also, consider the underscore library which makes all the stuff cross-platform: http://underscorejs.org/

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

7 Comments

node.js --> just one platform. As long as you do not need to run this in the browser you do not need cross-platform compatibility.
But nevertheless it is better to write cross-platform standards-compliant code, unless the costs are too high.
I think there are different approaches to this. For sake of performance I do not write cross-platform if it does not need to run cross-platform.
Wait, this isn't even correct. Did you even test it? Where is search defined?
@MatBee There was a mistype (fixed already). The point of writing the code on SO is not to make a production-ready library for the one who asked a question but rather to show an idea, a direction one should look into. I believe the mistype I accidentally made did not prevent anyone from understanding an idea and from understanding how to fix a mistype.
|

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.