Updating/Reassigning Object in Array #1277
Replies: 1 comment 2 replies
-
Updating arrays immutably is not very easy. This applies to not only jotai but const arr = [{ id: 1, name: 'React' }, { id: 2, name: 'Hooks' }]
// updating name of id=1 to "Jotai"
const index = arr.findIndex((item) => item.id === 1)
const newArr = [
...arr.slice(0, index),
{ ...arr[index], name: 'Jotai' },
...arr.slice(index + 1)
] |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Saw many examples of mutating objects and arrays but not an array of objects and the conventional methods don't seem to be working. Any help? Simply trying to find and update an object in the array.
`
Instead of Object.assign, I also tried deckList[index] = get(deckOperatorAtom).
Beta Was this translation helpful? Give feedback.
All reactions