How to do Array Push?


#1

Hello everyone,
I’m trying to push a value in an array. Example if a value does not exist in the array then do the push.

{
  "Command": "storeEval",
  "Target": "new Array(\"AAA\");",
  "Value": "arrayId"
},
{
  "Command": "storeEval",
  "Target": "storedVars['arrayId'].indexOf('BBB')",
  "Value": "indexFound"
},
{
  "Command": "if",
  "Target": "${indexFound} > -1",
  "Value": ""
},
{
  "Command": "storeEval",
  "Target": "storedVars['arrayId'].push('BBB')",
  "Value": ""
},
{
  "Command": "endif",
  "Target": "",
  "Value": ""
}

or something like that:

{
“Command”: “storeEval”,
“Target”: “new Array(“AAA”);”,
“Value”: “arrayId”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘arrayId’].indexOf(‘BBB’)”,
“Value”: “indexFound”
},
{
“Command”: “if”,
“Target”: “${indexFound} > -1”,
“Value”: “”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘arrayId’] [10] = ‘BBB’”,
“Value”: “”
},
{
“Command”: “endif”,
“Target”: “”,
“Value”: “”
}


#2

What exactly goes wrong? The DemoStoreEval macro contains a test case on how to use arrays and storedVars, here is the relevant part of this macro (starts at source line 154) :

{
  "Command": "storeEval",
  "Target": "new Array ('cat','dog','fish','dog','🐟','frog','dog','horse','??elephant')",
  "Value": "names"
},
{
  "Command": "storeEval",
  "Target": "storedVars['names'].length",
  "Value": "length"
},
{
  "Command": "echo",
  "Target": "array length = ${length}",
  "Value": ""
},
{
  "Command": "storeEval",
  "Target": "Math.floor(Math.random()*storedVars['length'])",
  "Value": "num"
},
{
  "Command": "echo",
  "Target": "num=${num}",
  "Value": ""
},
{
  "Command": "echo",
  "Target": "The next command picks the random item",
  "Value": ""
},
{
  "Command": "storeEval",
  "Target": "storedVars['names'][${num}]",
  "Value": "myrandomname"
},
{
  "Command": "store",
  "Target": "Today is ${mydate}, and we draw a ${myrandomname}",
  "Value": "output"
},