// 浅拷贝
let q10Viking = {
name: null,
school: null
}
let hzz = {
name: "Q10Viking",
school: 'CAU'
}
console.log(q10Viking,hzz)
let res = Object.assign(q10Viking,hzz)
console.log(res,q10Viking)
// 改变值
q10Viking.school="CIEE"
console.log(res,q10Viking)
/**
{name: null, school: null} {name: "Q10Viking", school: "CAU"}
VM190:14 {name: "Q10Viking", school: "CAU"} {name: "Q10Viking", school: "CAU"}
VM190:17 {name: "Q10Viking", school: "CIEE"} {name: "Q10Viking", school: "CIEE"}
*/
Last active
December 31, 2019 06:42
-
-
Save upangka/7a57fa442c9431404ae4d04242cc74f5 to your computer and use it in GitHub Desktop.
javascript 赋值对象相同属性的值
- 也不一定如果属性值为对象的话
// 深拷贝
let arra = {
school: '',
name: '',
age: ''
}
let arrb = {
school: 'CAU',
name: 'Q10Viking',
}
Object.keys(arra).forEach(key=>{
arra[key] = arrb[key] || arra[key]
})
console.log(arra)
arra.name="hzz"
console.log(arra,arrb)
/**
{school: "CAU", name: "Q10Viking", age: ""} {school: "CAU", name: "Q10Viking"}
VM353:17 {school: "CAU", name: "hzz", age: ""} {school: "CAU", name: "Q10Viking"}
*/
虽然key的赋值上面写很简单,但是如何要赋值的是零,这不会赋值成功,因为零为false, 过滤0
Object.keys(this.modifyDialogData).forEach(key => {
if (!(!val[key] && val[key] !== 0)) {
this.modifyDialogData[key] = val[key]
}
})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment