I have a unique situation that I'm struggling with. I have some data like this. Excuse any incorrect terms as I'm fairly new to this.
usersByName: { "tester": { 0: { id: 123 name: "Some Name" data: "Some data" }, 1: { id: 124 name: "Some Name 2" data: "Some data 2" }, 2: { id: 125 name: "Some Name 3" data: "Some data 4" }, }, "tester 2": { 0: { id: 12 name: "Some Name" data: "Some data" }, 1: { id: 13 name: "Some Name 2" data: "Some data 2" }, } }
I am returning a piece of data like this.
{ id: 44 name: "Some Name New" data: "Some data New" }
and a name like tester 2
.
How can I add the new piece of data to the name I have found so the new data gets added as a third element to tester 2
. I am using react and next js so hoping to add it directly to the state if possible. I think something like this but can't work out the obvious parts of it that are wrong
this.setState((prevState) => ({ usersByName: { // copy all other key-value pairs newuservar: { ...prevState.usersByName.newuservar, // copy all pizza key-value pairs X: newData, }, }, }))
https://stackoverflow.com/questions/66847558/inserting-array-item-into-js-object March 29, 2021 at 08:53AM
没有评论:
发表评论