1

I am using immutability-helper (https://github.com/kolodny/immutability-helper) to update state in Redux actions. I have an array and I'm adding an element like this:

update(state, { modalAlerts: { $push : [payload] } })

It works great. However, I don't know how to remove the last element of the array using Immutability Helper without knowing the size of the array. What is the equivalent of something like update(state, { $pop: modalAlerts } }) (it doesn't exist) to remove the last element?

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389

1 Answers1

1

Looking directly at the source code for immutability helper, it does not have what you are looking for. However you can extend it per the docs to add that functionality.

This then becomes a question of how to remove the last element of an array, which is already answered here with a few options including using pop, but can be as simple as:

arr.splice(-1, 1);

Since immutability helper does have a $splice command, that might enough.

Todd Chaffee
  • 6,754
  • 32
  • 41
  • I'll give it a try. However, I didn't get your last sentence regarding `splice` won't remove the last element, whereas you've said it might be enough in the previous sentence. – Can Poyrazoğlu May 18 '19 at 14:30
  • 1
    What I meant to say is that it will not remove the element if it is the only remaining element. And I'm pretty sure I was wrong about that because I just tested it. I removed that note from the answer. Thanks! – Todd Chaffee May 18 '19 at 15:46