JS/jQuery 遍历对象属性

Javascript For/In 循环: 循环遍历对象的属性
[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var person={fname:"John",lname:"Doe",age:25};  
  2.   
  3. for (x in person)  
  4.   {  
  5.     txt=txt + person[x];  
  6.   }  
  7. 结果:JohnDoe25 

也可以

for ( var i = 0;i < person.length; i++) {
document.write(person[i]+ "," );
}

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

[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var arr = ["one""two""three""four""five"];  
  2. var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };  

遍历数组

[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var text = "Array ";  
  2. jQuery.each(arr, function(i, val) {  
  3.     text = text + " #Index:" + i + ":" + val;  
  4. });  
  5. console.log(text);  
  6. //Array  #Index:0:one #Index:1:two #Index:2:three #Index:3:four #Index:4:five  
遍历对象
[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. text = "Object ";  
  2. jQuery.each(obj, function(i, val) {  
  3.     text = text + "Key:" + i + ", Value:" + val;  
  4. });  
  5. console.log(text);  
  6. //Object Key:one, Value:1Key:two, Value:2Key:three, Value:3Key:four, Value:4Key:five, Value:5 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值