首页 > 代码库 > JS/jQuery 遍历对象属性
JS/jQuery 遍历对象属性
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
JS/jQuery 遍历对象属性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。