js对象遍历和拷贝

let obj={name:'user1',age:12,gender:'man'}

for(let attr in obj){

  console.log(attr,obj[attr])

}

const keys=Object.keys(obj)

const values=Object.values(obj)

console.log(Object.keys(obj))
console.log(Object.values(obj))
 
    let obj={name:'user1',age:12,gender:'man'}
    let obj1={}
    let obj2=Object.assign({},obj)
    obj2.name='aaa'
    console.log(obj2,obj) //{name: 'aaa', age: 12, gender: 'man'}   {name: 'user1', age: 12, gender: 'man'}
posted @ 2023-05-26 11:03  howhy  阅读(28)  评论(0编辑  收藏  举报