您的位置:首页 > Web前端 > JQuery

JS/jQuery 遍历对象属性

2016-09-02 10:57 447 查看
Javascript For/In 循环: 循环遍历对象的属性

[javascript]
view plain
copy





var person={fname:"John",lname:"Doe",age:25};  
  
for (x in person)  
  {  
    txt=txt + person[x];  
  }  
结果:JohnDoe25 

也可以

jQuery jQuery.each() 遍历对象属性

[javascript]
view plain
copy





var arr = ["one", "two", "three", "four", "five"];  
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };  

遍历数组

[javascript]
view plain
copy





var text = "Array ";  
jQuery.each(arr, function(i, val) {  
    text = text + " #Index:" + i + ":" + val;  
});  
console.log(text);  
//Array  #Index:0:one #Index:1:two #Index:2:three #Index:3:four #Index:4:five  
d9d4

遍历对象

[javascript]
view plain
copy





text = "Object ";  
jQuery.each(obj, function(i, val) {  
    text = text + "Key:" + i + ", Value:" + val;  
});  
console.log(text);  
//Object Key:one, Value:1Key:two, Value:2Key:three, Value:3Key:four, Value:4Key:five, Value:5 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: