I have an array of objects that I have passed from JS to GS. Here is an example of how the array of object may look like (changes depending on user input)
var playersArray =
[
{number=1,
role=Arsonist,
deathStatus=false,
guardianTargetStatus=false,
executionerTargetStatus=false,
roleblockStatus=false},
{number=2,
role=Arsonist,
deathStatus=false,
guardianTargetStatus=false,
executionerTargetStatus=false,
roleblockStatus=false},
{number=3, role=Arsonist,
deathStatus=false,
guardianTargetStatus=false,
executionerTargetStatus=false,
roleblockStatus=false}
]
Throughout my code I need to call some data from this array. For example
var roleList = playersArray.map(function(role) {return role.role;});
Browser.msgBox(playersArray[1]["role"]);
When I pass this playersArray directly between functions, everything worls fine. Unfortunately for one of my functions I need to pull this array without passing it directly.
I have therefore set playerArray as a Property within GS
PropertiesService.getScriptProperties().setProperty('playersArray', JSON.stringify(playersArray));
I am then using getProperty inside relevant function to get this array:
var playersArray = PropertiesService.getScriptProperties().getProperty('playersArray');
playersArray = playersArray.replace(/\"/g, "").replace(/\:/g, "=");
playersArray = playersArray.split(",");
For some reason the function cannot see the array properly when I do that:
var roleList = playersArray.map(function(role) {return role.role;});
Browser.msgBox(playersArray[1]["role"]);
Please help me solve this. If the solution I am using for passing the array as global is not viable, I am happy to see other solutions. But it has to be global because I cannot pass the array directly between functions.