Reset all self switches in a map's events.

I have a certain map in my project that the player can enter and mess around with events. The events activate self switches (since I need specific switches per event due to a plugin I'm using) when interacted with. What I want to know is if it's possible that when the player exits the map all the self switches of every event in the map will be reset, so when they re-enter everything is back to normal. Is this possible?
put script in the exit event 

Code:
var m = $gameMap.mapId()
for (var s = 1; s < $dataMap.events.length; s++){
$gameSelfSwitches.setValue([m, s, 'A'], false);
$gameSelfSwitches.setValue([m, s, 'B'], false);
$gameSelfSwitches.setValue([m, s, 'C'], false);
$gameSelfSwitches.setValue([m, s, 'D'], false);}
Using $dataMap.events.length may not be a good approach, as there's no guarantee your events are going to be sequentially numbered.  If you create 20 events on the map and then delete event #3, you will have 19 events, but they will be numbered 1-2 and 4-20.  The above would only go 1-19.  Also, if you use an Erase Event command, does the event get removed from $dataMap.events?  That would cause a similar issue.


It would be more reliable to run a filter on $gameSelfSwitches (the _data variable, I think - I just cleaned my PC and reinstalled windows, and haven't put MV back on yet) to pull out those where the key starts with the correct map ID, and set them all to false.  Then you'll get ALL the events that have ever been on that map and have had a self switch turned on, even if they've been removed with Erase Event in the meantime.
even we delete event #3 it is still 20 and even if event #3 missed the script still working.

Original thread: https://forums.rpgmakerweb.com/threads/reset-all-self-switches-in-a-maps-events.59196/