javascript - 直接克隆对象属性/值

标签 javascript reactjs

我想将对象属性值克隆到空变量或空对象中。 我不知道我们是否可以实现它因此我有这个疑问我搜索但没有得到解决方案

事件有键 --> 名字、姓氏和年龄

我试过直接使用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/

相关文章:

javascript - 停止在表单字段中输入字符

javascript - ubuntu 13.10 和 xampp linux 1.8.3 中的 sfRenderException

javascript - React js 中的嵌入式 .doc 和 .txt 查看器

javascript - ReactJS - Prop 命名的首选约定

reactjs - 如何以高性能方式连接两个 React 应用程序

javascript - React JS,加载json数据然后操作它

javascript - 从 dojo 存储获取行数据

javascript - 选择所有具有指定字符串的div

javascript - 检测你在 JavaScript 上的哪个页面?

javascript - React 意外的 token ,预期为 "..."