Home > Mobile >  How to edit an item from my Reducer Objec based only with the ID?
How to edit an item from my Reducer Objec based only with the ID?

Time:06-07

I have a Reducer like this :

clientData : {
1080 : [{ID : 1111,name : 'John'},{ID : 2222,name : 'Stan'},],
1090 : [{ID : 3333,name : 'Adam'},{ID : 4444,name : 'Alan'},]
}

And I want to be able to edit the 444 Id's element like this :

{ID : 4444,name : 'New Value'}

So the output should look like :

clientData : {
1080 : [{ID : 1111,name : 'John'},{ID : 2222,name : 'Stan'},],
1090 : [{ID : 3333,name : 'Adam'},{ID : 4444,name : 'New Value'},]
}

Have you an idea please how to achieve it ? thank you

CodePudding user response:

you can try it :

const clientData = {
  1080 : [{ID : 1111,name : 'John'},{ID : 2222,name : 'Stan'},],
  1090 : [{ID : 3333,name : 'Adam'},{ID : 4444,name : 'Alan'},]
}
Object.values(clientData).forEach(ele => {
  let a = ele.find(el => el.ID === 4444)
  if (a) a.name = 'new value'
})
console.log(clientData)

  • Related