分享几种 javascript 遍历对象的方法
<script>
const obj = {
id:1,
name:'zhangsan',
age:18
}
for(let key in obj){
console.log(key + '---' + obj[key])
}
</script>
<script>
const obj = {
id:1,
name:'zhangsan',
age:18
}
Object.keys(obj).forEach((key)=>{
console.log(key+' : '+obj[key]);
});
</script>
<script>
const obj = {
id:1,
name:'zhangsan',
age:18
}
Object.getOwnPropertyNames(obj).forEach(function(key){
console.log(key+ '---'+obj[key])
})
</script>