例如使用 for-in 来循环对象的属性
var myObj = { "name":"runoob", "alexa":10000, "site":null };
for (x in myObj) {
document.getElementById("demo").innerHTML += x + "<br>";
}//name alexa site
在 for-in 循环对象的属性时,使用中括号([])来访问属性的值
var myObj = { "name":"runoob", "alexa":10000, "site":null };
for (x in myObj) {
document.getElementById("demo").innerHTML += myObj[x] + "<br>";
}
for-in 循环数组
var person1 = [{ fname: "Bill", lname: "Gates", age: 56 }, { fname: "aiaa", lname: "Bates", age: 43 }, { fname: "Gill", lname: "Bites", age: 11 }];
for (y in person1) {
console.log(person1[y]);//{ fname: "Bill", lname: "Gates", age: 56 },{ fname: "aiaa", lname: "Bates", age: 43 }, { fname: "Gill", lname: "Bites", age: 11 }
console.log(person1[y].fname)//Bill aiaa
}