The Inarray method in jquery: A Notice analysis _jquery

Source: Internet
Author: User

This article analyzes the Inarray method in jquery. Share to everyone for your reference, specific as follows:

jquery provides a great convenience for web developers, and the purpose of this article is to remind you to be cautious about the inarray approach in jquery.

As you all know, JavaScript is a weak type language, for numeric types and character types, you can switch at will (for example: 1+ "" = "1"), the following start to say the topic:

A method of jquery inarray (Ele, array) is to determine whether ele exists in the array, the return value is the first time the element appears in the array of subscript, no time to return-1.

For example:

var a = 1;
var array = [1,2,3];
var B = $.inarray (A, array);

At this point, b equals 0.

However, if you will a= "1" At this time, then do it again:

b = $.inarray (A, array);

At this point, B equals-1. This will create a problem, because some people may just want to get this judgment: "1" = 1, the logical expression is really in the JavaScript environment, the return is true, but, in Inarray, returns true false, so, if an array, stored in the non-object variable , especially when judging the numbers, it is best to use the original method of judgment.

You can also use regular expressions to verify the following:

var a = 1;
var reg = new RegExp (^ "+a+",) | ( ^ "+a+" $) | (, "+a+",) | (, "+a+");
var array = [1,2,3,4];
Reg.test (Array.tostring ());/true
a = "1";
Reg.test (Array.tostring ());/True

All right, just say so much.

More about jquery Arrays interested readers can view the site topics: "JQuery Array Usage Summary"

I hope this article will help you with the jquery program design.

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.