It's not complicated if you use events but you've chosen to use scripting, an additional feature which is programming in javascript. I don't think you can even access the array object in the way that you're attempting but I could be wrong.
You likely won't be able to use the usual methods like array.at(a,b) so probably it doesn't recognise MyArray at all, thought I've not tried this combination of events and javascript before. I assume you create the array yourself in code if you decide to go down the scripting method, something like const MyArray = [a,b,c]
If you can use such methods as above in the scripting like array.at(a,b) from the array object then this looks like a separate coding issue to do with scope of variables and accessing the array.