//遍历对象 4种方法
//Object.keys(obj).forEach()
console.log("keys...遍历</br>")
var obj1 = {
'0': 'a',
'1': 'b',
'2': 'c'
};
Object.keys(obj1).forEach(function (keys) {
console.log(keys, obj1[keys]);
}) //for(var i in obj){}
console.log("For...in... ")
var obj2 = {
'0': 'a',
'1': 'b',
'2': 'c'
};
for (var i in obj2) {
console.log(i, obj2[i]);
} //Object.getOwnPropertyNames(obj) console.log("Object.getOwnPropertyNames(obj)")
var obj3 = {
'0': 'a',
'1': 'b',
'2': 'c'
};
Object.getOwnPropertyNames(obj3).forEach(function (keys) {
console.log(keys, obj3[keys]);
}) //Reflect.ownKeys(obj).forEach 此乃es6新方法
console.log("Reflect.ownKeys(obj).forEach")
var obj4 = {
'0': 'a',
'1': 'b',
'2': 'c'
};
Reflect.ownKeys(obj4).forEach(function (keys) {
console.log(keys, obj4[keys]);
}) //遍历数组 3种方法
console.log("forEach")
var arr1 = [1, 2, 3, 4]; arr1.forEach(function (val, index) {
console.log(val, index);
}); console.log("for...in...")
var arr2 = [1, 2, 3, 4];
for (let i in arr2) {
console.log(i, arr2[i]);
} console.log("for...of...")
var arr3 = [1, 2, 3, 4];
for (let value in arr3) {
console.log(value);
}