Two methods of traversal in jquery

Source: Internet
Author: User


1.$.map (Array,callback (Element,index))
For each element in an array of arrays, call the callback () function, and eventually return a new array, the original array unchanged.
Case: Double the value of an element with an index greater than 3 in an array, the remaining value is unchanged, and a new array is returned
var arr = [1, 2, 4, 6, 3, 7, 8];
var newArr = $.map (arr, function (ele, index) {
if (Index > 3) {
return ele * 2;
}
else {
return ele;
}
});
alert (NEWARR);
2.$.each (ARRAY,FN)//Iterate through the array, return false to eject the loop
It is used primarily to iterate through an array, not to modify an array, or to have no problem with an ordinary array or a "key-value pair" array.
This can be used directly in each function to represent the value of the current element.
Key-value pairs
var dic = {"name": "Xiaoming", "Age": 23};
$.each (DIC, function (k,v) {
Alert (k + ' = = = ' +v);
//});

var arr = [1, 2, 3, 4, 5, 6];
$.each (arr, function (k, v) {
Alert (k + "= = =" +v);
});

Two methods of traversal in jquery

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.