Posted By

BFTrick on 09/22/10


Tagged

javascript array inarray


Versions (?)

In Array


 / Published in: JavaScript
 

URL: http://snook.ca/archives/javascript/testing_for_a_v

Simulates PHP in_array function.

['foo','bar'].has('foo') // returns true ['foo','bar'].has('foo',1) // returns 0

  1. Array.prototype.has=function(v,i){
  2. for (var j=0;j<this.length;j++){
  3. if (this[j]==v) return (!i ? true : j);
  4. }
  5. return false;
  6. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: dob on September 22, 2010

Array.prototype.has = function(e, i) { return i || !!~this.indexOf(e); }

You need to login to post a comment.