遍历对象属性及对其拷贝

  1. 遍历对象属性
let obj = {
  name: 'june',
  gendar: 'female',
  age: 23, 
}

Object.keys(obj).forEach(attr) => {
  console.log(obj[attr])
}

Object.keys return an array whose elements are string of obj attribute

the better answer

  1. 使用Object.assign 处理对象的拷贝

const object1 = {
  a: 1,
  b: 2,
  c: 3
};

const object2 = {
  c: 4,
};

const object3 = Object.assign({}, object1, object2);

Object.keys(object3).forEach(attr => {
  console.log(attr);
  console.log(object3[attr]);

})


open in code open

你可能感兴趣的:(遍历对象属性及对其拷贝)