js iterates over objects

json traverse object

 

 

arr= [

 

{"name":"wyy", "password":"1111"},

 

{"name":"tay", "password":"2222"}

 

];

 

for(var p in arr){//When traversing the json array, write p as the index, 0,1

 

   alert(arr[p].name + " " + arr[p].password);    // wyy   111     //tay  222

 

}

arr = {"name":"wyy", "xuehao":"4735"};

 

for(var p in myJson){ //traverse each key/value pair of the json object, p is the key

 

  console.log(p + " " + arr[p]);        //  name  wyy

                                          //xuehao 4735

 

} //aleat() displays an alert box with a message and ok button

 

iterate over keys and values

arr = {"name":"wyy", "xuehao":"4735"};

 

for(var p in myJson){ //traverse each key/value pair of the json object, p is the key

 

   alert(p + " " + arr[p]);        //  name  wyy

                                          //xuehao 4735

 

}

each iterates over objects

var obj = { one:1, two:2, three:3, four:4};     

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

    console.lo(obj[key]);           

});   

//  1 2 3 4 

 

$().ready(

function(){

var Object = {one:1,two:2,three:3};//For json array each

$.each(Object,function(name,value) { //name represents the object property name

                                                                      //value represents the value of the object property

console.lo(name);

console.lo(value);

});

 

}

);

 

  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326861090&siteId=291194637