Cocos JS Cc.each looping through objects

Source: Internet
Author: User

with it, mother no longer have to worry about my array will be out of bounds!!

Each () method makes the DOM loop structure concise and error-prone. The each () function encapsulates a very powerful traversal function and is handy for traversing one-dimensional arrays, multidimensional arrays, DOM, JSON, and so on.
The use of $each during JavaScript development can greatly reduce our workload.

Here are some common uses of each

Each handles a one-dimensional array
var arr1 = ["AAA", "BBB", "CCC"];
Cc.each (arr1, function (i,val) {
alert (i);
Alert (val);
}); alert (i) outputs the 0,1,2
Alert (val) will output AAA,BBB,CCC

Each handles two-dimensional arrays
var arr2 = [[' A ', ' AA ', ' aaa '], [' B ', ' BB ', ' BBB '], [' C ', ' cc ', ' CCC ']
Cc.each (arr, function (I, item) {
alert (i);
alert (item);
}); ARR2 is a two-dimensional array, and item is equivalent to each of the arrays in the two-dimensional array.
Item[0] Relative to the first value in each one-dimensional array
alert (i) outputs the output as 0,1,2 because the two-dimensional array contains 3 elements of the array
Alert (item) will output as [' a ', ' AA ', ' aaa '],[' b ', ' BB ', ' BBB '],[' C ', ' cc ', ' CCC ']

After a minor change to the handling of this two-bit array

var arr = [[' A ', ' AA ', ' aaa '], [' B ', ' BB ', ' BBB '], [' C ', ' cc ', ' CCC ']
Cc.each (arr, function (I, item) {
Cc.each (Item,function (j,val) {
Alert (j);
Alert (val);
});
});

Alert (j) will output as 0,1,2,0,1,2,0,1,2

Alert (val) will output as A,AA,AAA,B,BB,BBB,C,CC,CCC

Each processing JSON data, this each has more powerful, can loop each property

var obj = {one:1, two:2, three:3};
Cc.each (obj, function (key, Val) {
alert (key);
Alert (val);
}); Here alert (key) outputs one of the three
Alert (val) will output one,1,two,2,three,3
This way, why is key not a number but a property, because JSON is a set of unordered properties in the format -Value, since the disorder, and how to figure it.
And this val equates to Obj[key]

jquery automatically determines which elements are passed in, and then takes the handle of the Apply or call method based on the result of the decision. In the implementation of FN, you can use the this pointer to refer to an array or the child elements of an object directly.

1.obj objects are arrays

Each method makes an FN function call to each of the child elements of the array, until the call to a sub-element returns False, that is, we can exit each method call after the supplied FN function is processed so that it satisfies certain conditions. When the each method provides the arg parameter, the FN function call passes in the argument arg, otherwise: the child element Index, the child element itself

2.obj object is not an array

The biggest difference between this method and 1 is that the FN method will be carried out regardless of the return value. In other words, all properties of the Obj object will be called by the FN method, even if the FN function returns false. The call passed in with a parameter similar to 1.

Cocos JS Cc.each looping through objects

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.