简体   繁体   中英

Add an Object in a Object in React Native

i'm trying to add an object in my object vars.

Basically, i have two variables, the first named key with " time ", and the second " val " with an hours. And i'm trying to put this two variable in my 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}})

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? Thanks

You can use squared brackets to put the variables inside of an object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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