JS/jQuery 遍历对象属性

本文介绍了使用JavaScript中的For/In循环及jQuery.each()方法来遍历对象属性的方法,并通过实例展示了如何利用这些技术来获取对象的所有属性值。
Javascript For/In 循环: 循环遍历对象的属性
 
var person={fname:"John",lname:"Doe",age:25};  

 

for (x in person)  

 

  {  

   txt=txt + person[x];  

  }  

结果:JohnDoe25  

 

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

 var arr = ["one", "two", "three", "four", "five"];  

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

遍历数组

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  

遍历对象

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  

 
 

转载于:https://www.cnblogs.com/kivenlv/p/6038076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值