The method of loop traversal of object objects in jquery _jquery

Source: Internet
Author: User

A friend asked how the object was converted to an array, and when I asked him why he had to turn, he told me that the array could be iterated through with JS, while the object could not. In fact, objects can also loop through the AH. It can be cycled without conversion! Show that you are not very skilled in JS or jquery operations! Here I give a brief introduction!

Case

We look at the following objects:

var data={john:, Dick: A, Harry: A, two---------------------,
  Front Blog:
  haorooms:98, Wang Daozhong
  :

If the above is back in the background of a key, one is the object of value (this object believe that we often encounter it!) And now you want to loop this content into a table. Some friends on the object of the cycle do not know how to operate! Today I will introduce you to two methods!

Method One:

Cycle in a $.each way!

If you do not know $.each, suggest that you search the Internet. Of course, you can also search for "jquery's $ (). Each,$.each difference", about their differences, I am not in detail here. may have friends $ (). Each of this method used more, today mainly to introduce $.each.

$.each () can traverse arrays and objects. In the following ways:

$.each ([{"Name": "Haorooms", "email": "bb@126.com"},{"name": "Qianduan", "email": "aa@hao.com"}],function (i,n)
{
alert ("Index:" +i, "corresponding value is:" +n.name);
});

You can also traverse this:

var arr1 = ["One", "two", "three", "four", "five"];
$.each (arr1, function () {
alert (this);
});

Output: Onetwo three four five
The best thing is to iterate through the array:

The Var obj =data;//copy of the data
$.each (obj, function (key, Val) {
alert (obj[key));//can output the result
console.log (key); /Can output name
});

Method Two:

With a For In loop, you can traverse obj

To the object above, we can write this to the loop!

for (var i in data) {
 console.dir (i);//Output name
 Console.dir (Data[i]);//Output fraction
}

For In loop believe everybody should be no stranger! For loops we use a lot of the for (Var i=0;i<array.length;i++) so that this is mainly loop over the array. For objects, we usually use a for in loop!

In addition, if we encounter the following objects:

var data={john:, Dick: A, Harry: A, two---------------------,
  Front Blog:
  haorooms:98, Wang Daozhong
  :

We can use Data.haorooms to get 98, but we use "data. Front-End Blog" will be an error, therefore, when we use Chinese characters to do key value, when the selection should be used data["front-end blog", this way to choose, do not use the point number.

The above is the method of iterating the object in jquery, hope to be helpful to everybody's study.

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.