我对这个答案做出了贡献,因为我使用了一种稍微不同的方法来能够在步骤函数内部循环,而不必依赖 lambda 来递增。如果将来有人需要通用解决方案,这可以是一个很好的参考。
这是带有代码的示例:
{
"Comment": "A description of my state machine",
"StartAt": "InitVariables",
"States": {
"InitVariables": {
"Type": "Pass",
"Parameters": {
"index": 0,
"incrementor": 1,
"ArrayLength.$": "States.ArrayLength($.inputArray)"
},
"ResultPath": "$.iterator",
"Next": "LoopChoice"
},
"LoopChoice": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.iterator.ArrayLength",
"NumericGreaterThanPath": "$.iterator.index",
"Next": "IncrementVariable"
}
],
"Default": "End"
},
"IncrementVariable": {
"Type": "Pass",
"Parameters": {
"index.$": "States.MathAdd($.iterator.index, $.iterator.incrementor)",
"incrementor": 1,
"ArrayLength.$": "$.iterator.ArrayLength"
},
"ResultPath": "$.iterator",
"Next": "LoopChoice"
},
"End": {
"Type": "Pass",
"End": true
}
} }
这是循环的基础,我使用States.MathAdd($.iterator.index, $.iterator.incrementor)
用于添加两个值的内部函数,在本例中,使用 initVariables 状态中定义的增量来增加索引。并获取我想要循环的数组的长度。您还可以使用内部函数来获取数组长度,States.ArrayLength("$.path.to.array")
。该数组在输入中传递。
要获取数组的值,我们可以使用内部函数,States.ArrayGetItem($.inputArray, $.iterator.index)
。
所有自定义逻辑都应该放在loopChoice状态和IncrementVariable状态之间。
希望这对将来的人有帮助。