Determine if the JS array package contains an element ____JS

Source: Internet
Author: User
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)
2 {
3 for (i=0;i<this.length;i++)
4 {
5 if (this[i] = = e)
6 return true;
7 }
8 return false;
9 }

or view source print?

1 Array.prototype.in_array = function (e)
2 {
3 For (i=0;i<this.length && this[i]!=e;i++);
4 Return! (I==this.length);
5 }

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)
3 {
4 var r=new RegExp (this. S+e+this. S);
5 Return (R.test) (this. S+this.join (this. S) +this. S));
6 }

The use method is view source print?

1 var arr=["A", "B"];
2 alert (Arr.in_array ("a"))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.