About the $each (Object, function (p1, p2) usage of jquery

Source: Internet
Author: User

It allows you to iterate through the property values of an object, an array, and manipulate it.

Instructions for use

The each function does not have exactly the same effect as the type of the parameter:

1. Traversing objects (with additional parameters)

$.each (Object, function (P1, p2) {

This This here points to the current property value of the object in each traversal

P1;     P2; Accessing additional parameters

}, [' Parameter 1 ', ' parameter 2 ']);

2. Iterating through an array (with attachment parameters)

$.each (Array, function (P1, p2) {

This This here points to the current element of the array in each traversal

P1;     P2; Accessing additional parameters

}, [' Parameter 1 ', ' parameter 2 ']);

3. Traversing objects (no additional parameters)

$.each (Object, function (name, value) {

This This points to the value of the current property

Name Name indicates the current property of the object

Value Value that represents the current property of the object

});

4. Iterate through an array (no additional parameters)

$.each (Array, function (i, value) {

This This points to the current element

I I represents the array current subscript

Value Value represents the current element of the array

});

Here are some common uses of each of the jquery methods

JS Code
var arr = ["One", "one", "three", "four"];
$.each (arr, function () {
alert (this);
});
The results for each of the above outputs are: One,two,three,four

var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]
$.each (arr1, function (I, item) {
Alert (item[0]);
});
In fact, ARR1 is a two-dimensional array, item is equivalent to taking each one-dimensional array,
Item[0] Relative to the first value in each one-dimensional array
So the above each output is: 1 4 7


var obj = {one:1, two:2, Three:3, four:4};
$.each (obj, function (key, Val) {
Alert (Obj[key]);
});
This each one is more powerful, can loop each property
The output is: 1 2 3 4

About the $each (Object, function (p1, p2) usage of 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.