Array and object basic operations (JQuery)

Source: Internet
Author: User

First of all, you have to understand what an array is and what is an object.

Array: is a collection of ordered values.

Object: is an unordered collection of attributes, each of which has its own name and value.

The $ sign is a jquery character

1 Jquery.each (object, [callback]) iterates over an array or object2 Parameters:3 object: Objects or arrays that need to be repeated. 4Callback: The callback function that each member/element executes.5 To iterate over an array:6$.each (["Q", "W", "E", "R"],function(I, N) {//iterate through the array each ()7Alert ("Item:" + i + ":" + N)//i is an array ordinal, n is the corresponding value8             })9 Ten  One  A To traverse an object: -$.each ({name: "Qqj", Lang: "JS"},function(i, n) { -              //Traverse Object {name:name, value:qqj} theAlert ("Name:" + i + "\nvalue:" + N);//I for the name n is the value -})
Jquery.extend ( [Deep], Target, Object1, [OBJECTN]) Extension Object

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

If target is not specified, the jquery namespace itself is extended. This helps plug-in authors add new methods to jquery. If the first argument is set to True, jquery returns a deep copy, recursively copying any objects found. Otherwise, the copy will share the structure with the original object. Undefined properties will not be copied, but properties inherited from the object's prototype will be copied.

target: An object if the attached object is passed to this method will then it will receive the new attribute if it is the only parameter that will extend the jquery namespace.

Object1: The object to be merged into the first object.

objectn: The object to be merged into the first object.

 varOb1 = {name: "QQ", AGE:25, tt: "Uenuhs", qq:676373 }; varOB2 = {name: "Rain", Age:33, ww: "ww", EE: "ee" };                                          $.extend (Ob1, OB2); //The extension object, the same property, is modified later, the previous one is not added            varText = ""; $.each (Ob1,function(i, N) {//i is the name, n is the value text+ = ("Name:" + i + "value:" +n+ "\ n"); })
Output is Name:name
Value:rain
Name:age
Value:33
Name:tt
Value:uenuhs
Name:qq
value:676373
Name:ww
Value:ww
Name:ee
Value:ee
Jquery.grep (Array, callback, [Invert])

Filter the array elements using the filter function.

Array: arrays to be filtered.

callback: This function handles each element of the array. The first parameter is the current element, the second parameter, and the element index value. This function should return a Boolean value.

Invert: If "invert" is false or set, the function returns the element in the array that is true by the filter function, and when "invert" is true, returns the set of elements that return false in the filter function.

1 function (n,i) {  //n is an array value, I is an array ordinal 2   return n > 0; 3 });      // The default is false to    return an array that satisfies the condition
View CodeJquery.makearray (obj)

Converts an array of classes object to a group object.

The class array object has the Length property and its member index is 0 to length-1. In practice, this function is automatically used in JQuery without the need for intentional conversion.

1 <div>First</div><div>Second</div><div>Third</div><div> fourth</div>       // This is a 4 div element in HTML 2var arr = Jquery.makearray ( document.getElementsByTagName ("div")); 3 // using the array rollover function 4 Results: Fourth 5 Third 6 Second 7 First
View CodeJquery.map (Arr|callback)

Converts an element in an array to another array.

Array: arrays to be converted.

callback: called for each array element, and the conversion function is passed a representation of the transformed element as an argument. function to return any value.

1      vararr=[3,4,5,6,7,8]    2  varARR3 = $.map (arr,function(n) {//returns the new array map after the ARR array has been processed3                 returnn + 2; n array element value4             });5$.each (ARR3,function(n, i) {6alert (i);//Output 5,6,7,8,9,107})
View CodeJquery.inarray (Value,array,[fromindex])

Finds the position of an element in an array

value: The element that is used to find the presence in the array

Array: The arrays are being looked up.

fromIndex: Used to search the array queue with the default value of 0.

1 var arr=["QQ", "ww", "ee", "RR", 3,6,9]; 2 $.inarray ("QQ", arr);     // returns 0  , first ordinal value 3 $.inarray ("ee", arr,3)   // returns-1, retrieving from 3, 1 indicates no found 4 $.inarray ("RR", arr)    // return 3    , ordinal 3 in the array
View CodeJquery.merge (First,second)

Merging two arrays

First: An array that is to be processed will change the elements in it.

Second: The second pending array does not change the elements in it.

1 var arr1=["A", "B", "C"]; 2 var arr2=[1,2,3]; 3 $.merge (ARR1,ARR2);    // attaching arr2 to arr1 4    // then arr1 is arr1=["a", "B", "C", and "n";    arr2=[1,2,3]   unchanged
View Code

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.