1

enter image description hereI have done a lot of research and I saw a lot of similar questions but non answer this. I am trying to update a nested array, I have tried a lot of different variations but non work.

onChange={(e) => setUser([{ ...User[index], permissions : {CreateSubs : e.target.value} }])}

Right now this of course erases the other items I have inside of permissions arr, how do I just update the specific one I need?

     [
      {
        User: {
          name: "Name",
          permissions: {
            CreateSubs: true,
            CreateOtherStuff: false,
          },
        },
      },
    ]

Update

All the other article i found are not using react hooks, and it's a bit different

Below is what worked for me

onChange={(e) => setUser([ { ...User[index], permissions: { ...User[index].permissions, CreateSubs: e.target.checked } } ])}
JoelE
  • 169
  • 3
  • 12
  • try using Array.map function, with condition like ```array.map(function (a) { if(a.user.name === '') { return { ...a, user: {...a.user, permission:{ ...a.user.permission, }}}}})``` – kannandv Aug 12 '21 at 18:45
  • I'm assuming you're using `useState` for this? i.e., `const [user, setUser] = useState({}); ... setUser(...)} /> ` Your data model isn't totally clear here. You said you have a permissions array, but permissions is an object. And User has a capital U in the first snippet but a lower case u in the second one – Mike Young Aug 12 '21 at 18:53
  • @MikeYoung that was a mistake, i wrote it manually in the post, ill update it. and yes im using const [user, setUser] = useState({}); – JoelE Aug 12 '21 at 19:00
  • 1
    I finally figured it out `onChange={(e) => setUser([ { ...User[index], permissions: { ...User[index].permissions, CreateSubs: e.target.checked } } ])} ` – JoelE Aug 12 '21 at 21:17

0 Answers0