Introduction to jQuery's inArray Method

Source: Internet
Author: User

For example:
Copy codeThe Code is as follows:
$. Get ('aaaaa. ashx', null, function (d ){
// Assume that the value returned by d is 1, 3, 54,67.
Var arr = d. split (',');
$. InArray (3, arr) =-1 // true
// Why?
// If you write
Var arr = eval ('[' + d + ']');
$. InArray (3, arr)>-1 // true
});

Why? Hope you will know your friend and reply.
Jquery inarray () Functions
Jquery. inarray (value, array)
Determine the position of the first parameter in the array (-1 is returned if no value is found ).

Determine the index of the first parameter in the array (-1 if not found ).
Return Value
Jquery
Parameters
Value (any): used to search for existence in the array
Array (array): the array to be processed.

A friend asked a question today, as shown below:
Copy codeThe Code is as follows:
Var testarr = [{"a": "0" },{ "B": "1" },{ "c": "2"}]; alert ($. inarray ({"a": "0"}, testarr ));
  
This value always returns-1;
At first glance, I did not notice it, so I wrote a paragraph to show it to him.
Copy codeThe Code is as follows:
Var obj = {'M': '1'}; var arr = [obj, '1', 2]; alert ($. inarray (obj, arr ));

The returned value is normal.
Later I realized that the object is of the reference type.
You can use a short program to demonstrate the features of the reference type.

Copy codeThe Code is as follows:
Var obj = {"a": 0}; var obj1 = {"a": 0 };
Alert (obj = obj1); // false ;---------------------
Var obj = {"a": 0 };
Var obj1 = obj;
Alert (obj = obj1 );
// True;

Related Article

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.