0

如何将元素添加到 Kantu 中的现有数组

我执行以下代码,但 name2、length2、namesContent2 与预期不符

{
  "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": ""
    }
  ]
}

这是输出,但我期望一个长度为 10 的新数组

这个怎么做?

[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)

第一个数组没问题,它的长度为 9 个元素

我想向这个现有数组添加几个新元素(稍后在代码循环中对它们进行处理。

但是第二个数组只有两个元素,第一个元素是旧数组,第二个元素是新添加的元素

如何正确做到这一点?

4

1 回答 1

0

如果要保持原始数组不变,可以使用concat将新元素推送到旧数组的副本。

所以

{
  "Command": "storeEval",
  "Target": "[storedVars['names'],'Thomas']",
  "Value": "names2"
}

转向

{
  "Command": "storeEval",
  "Target": "storedVars['names'].concat('Thomas')",
  "Value": "names2"
},

输出:

[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
于 2019-08-28T09:25:10.133 回答