JavaScript Object traversal

Source: Internet
Author: User

first, the object is an array:  var arr = [{"id": "1", "TagName": "Apple"},{"id": "2", "TagName": "Orange"},{"id": "3", "TagName": "Banana"},{"id": "4", " TagName ":" Watermelon "},{" id ":" 5 "," TagName ":" Pineapple "}]; $.each (arr, function (index, obj) {
alert (index);              = = 0,1, 2, 3, 4 alert (obj);           =>{"id": "1", "TagName": "Apple"},{"id": "2", "TagName": "Orange"}...alert (obj.id);     = 1, 2, 3, 4, 5alert (Obj.tagname); + Apple, orange, banana, watermelon, pineapple
}); $.each (arr, function (index, obj) {alert (TagName of "+ Index +" is "+obj.tagname");

}); the tagname of the No. 0 item is Apple The tagname of the c8> 1th is orange ...... .....

=============================================================================================================== ============ Vararr = [9, 8,  7, 6 , 5 , 4 , 3 , 2 , 1 ];  Each traversal:$.each (Arr,function (index,value) {
alert ( index )}) =>0 1 2 3 4 5 6 7 8
$.each (Arr,function (index,value) {
alert ( value )
}) =>9 8 7 6 5 4 3 2 1 $.each (Arr,function () {
alert ( this )
}) =>9 8 7 6 5 4 3 2 1---------------------------------------------------- ------------------------------------------------------------------------------------For traversal:for  (Var i in arr)  {               //Not recommended for in traversal array       console.log (Arr[i]);   }          &NB Sp                          ,         &NB Sp           &NBSP;=&GT;9 8 7 6 5  4 3 2 1       for  (var i& nbsp;= 0; i < arr.length; i++)  {    console.log (Arr[i]);    }                                                              =>9 8 7 6 5  4 3 2 1    &nbsp ;                           ===================================== ======================================================================================  
vardata=[{"1":{"name":"kc","status":1}},{"2":{"name":"bc","status":2}}];  native JS gets:

for ( var i=0;i<data.length;i++){

     for ( var item  in data[i]){ //{"1":{"name":"kc","status":1}},{"2":{"name":"bc","status":2}}

         console.log( item ); // key     1,2

         console.log(data[i][item]); // value      {name: "kc", status: 1},{name: "bc", status: 2}

         console.log(data[i][item].name); //KC BC Get property value

         console.log(data[i][item].status); //1 2    获取属性值

     }

}

--------------------------------------------------------------------------------------------------------------- -------------------------


jquery get

     $.each(data,function(key,val){

     console.log(key); //索引

     console.log(val); //{"1":{"name":"kc","status":1}},{"2":{"name":"bc","status":2}}

     $.each(val,  function (key1,val1) {    

         console.log(key1); //key  1,2

         console.log(val1); //value, {name: "kc", status: 1},{name: "bc", status: 2}

         console.log(val1.name); //属性值

     });

})

=============================================================================================================== =============================================================================================================== ======================== second, the object is JSON:  var json = {"Jim": "One", "Tom": "N", "Lilei": "13"};jquery Get$.each (JSON, function (key, value) {
alert (key);               =>jim Tom Lilei Alert (value); =>11
});Native JS GetFor (var i in JSON) {alert (i); =>jim Tom Lilei    Alert (Json[i]); =>11 12 13};
=============================================================================================================== =============================================================================================================== ======================== Three, JSON is a string:(Solution: A JSON string is converted to a JavaScript object.) var json = ' [{' id ': ' 1 ', ' tagName ': ' Apple '},{' id ': ' 2 ', ' tagName ': ' Orange '},{' id ': ' 3 ', ' tagName ': ' Banana '},{' id ': ' 4 ', "TagName": "Watermelon"},{"id": "5", "TagName": "Pineapple"}] '; $.each ($.parsejson (JSON), function (idx, obj) {// $.parsejson (JSON)or json.parse (JSON)convert it to a JavaScript object string goto JSON objectalert (obj.tagname);            =>apple, orange, banana, watermelon, pineapple});

the parsing method of JSON  

There are two ways to parse JSON: eval () and Json.parse (), using the following methods:

var jsondata = ' {' data1 ': "Hello", "data2": "World"} '; var evaljson=eval ("(" +jsondata+ ")");             //Object {data1: "Hello", data2: "World"} var jsonparsejson=json.parse (Jsondata); Object {data1: "Hello", data2: "World"}

This converts the Jsondata JSON-formatted string into a JSON object.

 var dataobj=eval ("(" +data+ ")");//Convert to JSON objectEval Executes the code in the string when parsing a string (the consequences are pretty bad)
var obj= [{"id": "1", "TagName": "Apple"},{"id": "2", "TagName": "Orange"},{"id": "3", "TagName": "Banana"},{"id": "4", " TagName ":" Watermelon "},{" id ":" 5 "," TagName ":" Pineapple "}]; Json.stringify (obj)// JSON Object Turn string and Json.parse () instead  "[{"id": "1", "TagName": "Apple"},{"id": "2", "TagName": "Orange"},{"id": "3", "TagName": "Banana"},{"id": "4", " TagName ":" Watermelon "},{" id ":" 5 "," TagName ":" Pineapple "}]"

JavaScript Object traversal

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.