简体   繁体   English

在 React Native 中的 Object 中添加 Object

[英]Add an Object in a Object in React Native

i'm trying to add an object in my object vars.我正在尝试在我的 object 变量中添加一个 object。

Basically, i have two variables, the first named key with " time ", and the second " val " with an hours.基本上,我有两个变量,第一个带有“ time ”的命名键,第二个带有小时的“ val ”。 And i'm trying to put this two variable in my object this.state.vars .我正在尝试将这两个变量放在我的 object this.state.vars In the end, i want to have something like this最后,我想要这样的东西

"vars":{"time":"23:11"}

I already tried to do something like this.setState({vars: {keys: val}})我已经尝试过这样做了this.setState({vars: {keys: val}})

but it's not working.但它不起作用。 Do i need to do a specific call to add a new key and a new value in a Object?我是否需要进行特定调用才能在 Object 中添加新键和新值? Thanks谢谢

You can use squared brackets to put the variables inside of an object:您可以使用方括号将变量放入 object 中:

this.setState({vars: {...this.state.vars, [keys]: [val]}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM