How to add a Element to existing Array in Kantu
i do the following code, but name2, length2, namesContent2 are not as expected
{
"Name": "testArrayPush",
"CreationDate": "2019-8-28",
"Commands": [
{
"Command": "storeEval",
"Target": "new Array ('cat','dog','fish','dog','🐟','frog','dog','horse','??elephant')",
"Value": "names"
},
{
"Command": "storeEval",
"Target": "storedVars['names'].length",
"Value": "length"
},
{
"Command": "storeEval",
"Target": "storedVars['names']",
"Value": "namesContent"
},
{
"Command": "echo",
"Target": "array names = ${namesContent}",
"Value": ""
},
{
"Command": "echo",
"Target": "array length = ${length}",
"Value": ""
},
{
"Command": "storeEval",
"Target": "[storedVars['names'],'Thomas']",
"Value": "names2"
},
{
"Command": "storeEval",
"Target": "storedVars['names2'].length",
"Value": "length2"
},
{
"Command": "storeEval",
"Target": "storedVars['names2']",
"Value": "namesContent2"
},
{
"Command": "echo",
"Target": "array names2 = ${namesContent2}",
"Value": ""
},
{
"Command": "echo",
"Target": "array length2 = ${length2}",
"Value": ""
}
]
}
this ist the output, but i expected a new array of length 10
how to do this?
[status]
Playing macro testArrayPush
[info]
Executing: | storeEval | new Array ('cat','dog','fish','dog','🐟','frog','dog','horse','??elephant') | names |
[info]
Executing: | storeEval | storedVars['names'].length | length |
[info]
Executing: | storeEval | storedVars['names'] | namesContent |
[info]
Executing: | echo | array names = ${namesContent} | |
[echo]
array names = cat,dog,fish,dog,🐟,frog,dog,horse,??elephant
[info]
Executing: | echo | array length = ${length} | |
[echo]
array length = 9
[info]
Executing: | storeEval | [storedVars['names'],'Thomas'] | names2 |
[info]
Executing: | storeEval | storedVars['names2'].length | length2 |
[info]
Executing: | storeEval | storedVars['names2'] | namesContent2 |
[info]
Executing: | echo | array names2 = ${namesContent2} | |
[echo]
array names2 = cat,dog,fish,dog,🐟,frog,dog,horse,??elephant,Thomas
[info]
Executing: | echo | array length2 = ${length2} | |
[echo]
array length2 = 2
[info]
Macro completed (Runtime 5.47s)
the first array is ok, it has a length of 9 Elements
i want to add several new Elements to this existing array (and later in the code loop over them.
But the second array has only two elements, the first element is the old array and the second element is the newly added Element
How to to this correctly?
If you want to keep the original array unchanged, you could use concat to push the new element to a copy of the old array.
So
{
"Command": "storeEval",
"Target": "[storedVars['names'],'Thomas']",
"Value": "names2"
}
turns to
{
"Command": "storeEval",
"Target": "storedVars['names'].concat('Thomas')",
"Value": "names2"
},
Output:
[info]
Executing: | storeEval | storedVars['names'].concat('Thomas') | names2 |
[info]
Executing: | storeEval | storedVars['names2'].length | length2 |
[info]
Executing: | storeEval | storedVars['names2'] | namesContent2 |
[info]
Executing: | echo | array names2 = ${namesContent2} | |
[echo]
array names2 = cat,dog,fish,dog,🐟,frog,dog,horse,??elephant,Thomas
[info]
Executing: | echo | array length2 = ${length2} | |
[echo]
array length2 = 10