1.数组遍历
var arr = ['red','green','blue'];
for (var i=0;i<arr.length;i++){
console.log(arr[i]);
}
2.对象遍历
//第一种
const obj = {
id: 1,
name: "zhangsan",
age: 18,
};
for (let key in obj) {
console.log(key + "---" + obj[key]);
}
//第二种
const obj1 = {
id: 1,
name: "zhangsan",
age: 18,
};
console.log(Object.keys(obj1));
console.log(Object.values(obj1));
//第三种
const obj2 = {
id: 1,
name: "zhangsan",
age: 18,
};
Object.getOwnPropertyNames(obj2).forEach(function (key) {
console.log(key + "---" + obj2[key]);
});