js 对象遍历方法整理

js yekong

for…in…

var obj = {'0':'a', '1':'b', '2':'c'};
for(let i in obj){
    console.log(i,":",obj[i]);//{0:a,1:b,2:c}
}

Object.keys

var obj = {'0':'a', '1':'b', '2':'c'};
Object.keys(obj).forEach(function(key){
	console.log(key,obj[key]);//{0:a,1:b,2:c}
}

getOwnPropertyNames

var obj = {'0':'a','1':'b','2':'c'};

Object.getOwnPropertyNames(obj).forEach(function(key){
    console.log(key,obj[key]);
});
喜欢