This is probably a simple question, but my lack of JavaScript skills is probably preventing me from figuring out a one-liner to do this. Im using axios to request from an API, im getting a response back (This is for a "Todo" list app), however I need to merge another field into this response.
The snippet of code looks like this:
let mergedIsEditing = {...response.data, isEditing: false}
const lists = [ ...this.state.lists, mergedIsEditing ]
lists
is basically what it sounds like (an array of lists), and the response.data
is nothing more than an object with a title
and description
. I thought maybe using Object.assign
but since im already spreading out this.state.lists
im not sure how that would work. Essentially I just need to add the key/value pair isEditing: false
onto that list coming in.
Not to encourage shorter code over readability, but to answer your question, this is how you could do the equivalent to your snippet in 1 line:
let mergedIsEditing = {...response.data, isEditing: false}
const lists = [ ...this.state.lists, mergedIsEditing ]
const lists = [ ...this.state.lists, {...response.data, isEditing: false} ]
Of course, you could prepend this.state.lists =
, if that's the desired purpose.
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.