let obj1={
name:'111',
age:20,
sex:1
}
let obj2={
name:'222',
age:45
}
Object.keys(obj1).forEach(i=>{
if(obj2[i]){
obj1[i]=obj2[i]
}
})
console.log(obj1)//{name: "222", age: 45, sex: 1}
let obj1={
name:'111',
age:20,
sex:1
}
let obj2={
name:'222',
age:45
}
Object.keys(obj1).forEach(i=>{
if(obj2[i]){
obj1[i]=obj2[i]
}
})
console.log(obj1)//{name: "222", age: 45, sex: 1}
本文标题:两个Object根据key赋值合并(Object.keys)
本文链接:https://www.haomeiwen.com/subject/cmkqjltx.html
网友评论