我想将对象属性值克隆到空变量或空对象中。 我不知道我们是否可以实现它因此我有这个疑问我搜索但没有得到解决方案
事件有键 --> 名字、姓氏和年龄
我试过直接使用set state方法设置状态
this.setState({currentEvent:event});
完整代码如下:
this.state = {
dialogEvent:false,
currentEvent:{},
}
}
handleClickButton = event => {
// event is the single event that needs to be updated in parent event
after updating age.
console.log(event);
axios.get(configs.Data.getNewAge)
.then(res=>{
console.log(res); // res.data.newObj has time date and time epoch
Object.keys(event).forEach(function(index){
console.log(index);
}); // checking the keys for event by logging
this.setState({currentEvent:event});
this.setState({eventDialog:true}); // this is the dialog
}).catch((e)=>console.log("Error :"+e));
};
实际结果:如果在对话框中获取,currentEvent 是未定义的
预期结果:它应该克隆所有属性和值
最佳答案
使用spread .
const event = {firstName:"fn",lastName:"ln",age:99}
let currentEvent = {...event}
console.log(currentEvent)
关于javascript - 直接克隆对象属性/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55396493/