What is the preferred method for setting the value of a Seamless Immutable Array?

921 Views Asked by At

The docs for Seamless Immutable Object has set to update the value of a key in an object, but in the Immutable Array docs, there is no set for updating an element of an array.

What is the preferred method for setting the value of a Seamless Immutable Array?

1

There are 1 best solutions below

0
Beau Smith On BEST ANSWER

So I dug into the code — lo and behold! — there are undocumented Array methods:

My Redux reducer using the arraySet on the currentAnswer array:

export const updateIncompleteAnswer = (state, { assignment_id, questionIndex, questionAnswer }) => {
  const currentAnswer = state.incompleteAnswers[assignment_id]
  return state.merge({
    incompleteAnswers: {
      [assignment_id]: currentAnswer.set(questionIndex, questionAnswer)
    }
  })
}