To determine if the JS array package contains an element2010-06-21 15:10:21.0 575 people like this article
To determine if an array contains an element, from the principle, is to traverse the entire array, and then to determine whether equality, we build a wheel, name on the cottage PHP array function in_array () View source print?
1 |
Array.prototype.in_array = function (e) |
3 |
for (i=0;i<this.length;i++) |
or view source print?
1 |
Array.prototype.in_array = function (e) |
3 |
For (i=0;i<this.length && this[i]!=e;i++); |
4 |
Return! (I==this.length); |
Both of these are possible. is actually a form, the writing is not the same. And this is what Daniel wrote. View Source print?
1 |
Array.prototype.s=string.fromcharcode (2); |
2 |
Array.prototype.in_array=function (e) |
4 |
var r=new RegExp (this. S+e+this. S); |
5 |
Return (R.test) (this. S+this.join (this. S) +this. S)); |
The use method is view source print?
2 |
alert (Arr.in_array ("a")) |