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?

1 Answer 1

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.

2
  • 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. Commented May 18, 2019 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! Commented May 18, 2019 at 15:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.