Operation of an array in jquery

Source: Internet
Author: User

1.each for traversing objects and arrays

1.1 Iterating through an array

$.each (Arr,function (Index,value))

1.2 Traversing JSON

The index in the callback function is an object member, and value is a variable. such as {name:ll,age:24} Name,age is an object member, the latter is a variable

To exit each, you need to use return false in the callback function

Instance:

<script type= "Text/javascript" >
var arr=[2,1,4,7,9,14,87,78];
$ (function () {
$.each (Arr,function (index,value) {
if (index==5)
{
Return false;//exit each

}
Else
{

document.write ("arr[" +index+ "]:" +value+ ";")

};


}) ;

})

</script>

2.extend

Extends an object with one or more other objects, returning the object being extended.

$.extend (target, Object1, [objectn]) merges the following object into target, with the same object members, different variables, The variable of the following object overrides the previous object variable and assigns the merged object to the target

Example: (from Jqueryapi)

Merge defaults and options without modifying the defaults.

JQuery Code:
var empty = {};var defaults = { validate: false, limit: 5, name: "foo" };var options = { validate: true, name: "bar" };var settings = jQuery.extend(empty, defaults, options);
Results:
settings == { validate: true, limit: 5, name: "bar" }empty == { validate: true, limit: 5, name: "bar" }

)

3.grep

Used to filter the array, returning the array object according to the conditions given in the callback function

$.grep (Arr,function (value,index) {},boolean)

The Boolean default is False, which causes the function to return a new array that meets the criteria, and if ture, the opposite

4.inArray

Used to find the element in the array, similar to the string in JS search (str.search) search (RE) also return index, if not found also return-1

$.inarray (Value,array)

5.isArray (arr) returns a Boolean value that determines whether an object is not an array

6map

$.map (arr,function (value) {})

An array object passes the condition in the callback function to get a new array and returns the array similar to grep, and if the values of each array are arrays, the merge of the arrays will be produced

For example

JQuery Code:
$.map( [0,1,2], function(n){  return [ n, n + 1 ];});
Results:
[0, 1, 1, 2, 2, 3]

7.merge

Used to merge two of arrays

$.merge (ARR1,ARR2);

The function returns a new array (allowing duplicate array elements to exist)

8.unique (arr)

Can be used for DOM objects, or for arrays (references)

var Yeararray = new Array( in - );
$. Unique(Yeararray); return to the var Yeararray = new Array( in - );
Yeararray. sort();
$. Unique(Yeararray); Back to 

9.parseJSON (problematic)

Resolves a JSON string to an object

$.parsejson (' {' OBJECT ': VALUE} ')

{"name":"John"} name一定是双引号

10, ToArray

Restores all DOM elements in the jquery collection to an array.

$ ("tag"). ToArray ();

Operation of an array in jquery

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.