1.遍历对象的方法
var obj = { 'a': 1, "b": 2 , 'c': 3 };
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
for (var i in obj) {
console.log(i + ' ' + obj[i])
}
Object.getOwnPropertyNames(obj).forEach(function(key) {
console.log(key + ':' + obj[key]);
})
Reflect.ownKeys(obj).forEach(function(key) {
console.log(key + ':' + obj[key]);
})
1.遍历数组的方法
var arr = ['w', 'e', 'n'];
for (var [key, value] of arr.entries()) {
console.log(key, value);
}
for (var key of arr.keys()) {
console.log(key);
}
for (var value of arr.values()) {
console.log(value);
}
arr.forEach(function(value, key) {
console.log(key, value);
})
for (var i in arr) {
console.log(i, arr[i]);
}
for (var value of arr) {
console.log(value);
}
arr.map(function(value, index) {
console.log(index, value);
})