Sometimes, you need to empty your collections. For example, if the user navigates to a different part of the application, you could be left with collection data that you no longer need. Instead of deleting the collection reference, you could just empty it.
Emptying collections
Replacing collections with new instances
The simplest way to empty a collection is to replace it with a new instance, as shown here:
const empty = (collection) => {
if (collection instanceof List) {
return List();
}
if (collection instanceof Map) {
return Map();
}
return null;
};
const myList = List.of(1, 2);
const myMap = Map.of('one', 1, 'two', 2);
const myEmptyList = empty(myList);
const myEmptyMap = empty(myMap);
console...