我的代码中有问题,我不知道如何解决,我解释了情况,我有两个对象数组;我们需要执行一个循环的有效负载,如果它与名为可用的数组中的一项进行数学运算,我们需要用循环中的值减去此匹配的数量,例如这个数字(61327)来自有效负载并存在于可用中如果有效负载中的数量等于或小于可用数组中的数量,我们减去可用数组中的值,否则我们什么都不做,我们将结果保留在内存中以供下一个迭代器使用。
可用
[
{
"number": "61327",
"qty": 1
},
{
"number": "6010550",
"qty": 1
},
{
"number": "6010482",
"qty": 1
},
{
"number": "6228245",
"qty": 1
},
{
"number": "6076532",
"qty": 1
}
]
有效负载
[
{
"number": "61327",
"qty": 1
},
{
"number": "6010550",
"qty": 1
},
{
"number": "6010482",
"qty": 1
}
]
代码
%dw 2.0
output application/json
fun updateValueFromAvailable( list, sku, qty ) = list map ( itm, idx ) -> (if ( itm.number ~= sku ) itm update { case .wms_qty -> itm.wms_qty - qty } else itm )
---
payload map do {
var result = updateValueFromAvailable( available, $.number, $.qty )
---
$
}
我不知道是否可以省略结果的调用,或者有另一种方法来运行函数来更新而不调用它
可用的当前结果不会在下一次迭代中保留更新
[
[
{
"number": "61327",
"wms_qty": 0,
"item_id": "61327"
},
{
"number": "6010550",
"wms_qty": 1,
"item_id": "6010550"
},
{
"number": "6010482",
"wms_qty": 1,
"item_id": "6010482"
},
{
"number": "6228245",
"wms_qty": 1,
"item_id": "6228245"
},
{
"number": "6076532",
"wms_qty": 1,
"item_id": "6076532"
}
],
[
{
"number": "61327",
"wms_qty": 1,
"item_id": "61327"
},
{
"number": "6010550",
"wms_qty": 0,
"item_id": "6010550"
},
{
"number": "6010482",
"wms_qty": 1,
"item_id": "6010482"
},
{
"number": "6228245",
"wms_qty": 1,
"item_id": "6228245"
},
{
"number": "6076532",
"wms_qty": 1,
"item_id": "6076532"
}
],
[
{
"number": "61327",
"wms_qty": 1,
"item_id": "61327"
},
{
"number": "6010550",
"wms_qty": 1,
"item_id": "6010550"
},
{
"number": "6010482",
"wms_qty": 0,
"item_id": "6010482"
},
{
"number": "6228245",
"wms_qty": 1,
"item_id": "6228245"
},
{
"number": "6076532",
"wms_qty": 1,
"item_id": "6076532"
}
]
]
预期结果一个结果在所有迭代中保持更新
[
{
"number": "61327",
"wms_qty": 0,
"item_id": "61327"
},
{
"number": "6010550",
"wms_qty": 0,
"item_id": "6010550"
},
{
"number": "6010482",
"wms_qty": 0,
"item_id": "6010482"
},
{
"number": "6228245",
"wms_qty": 1,
"item_id": "6228245"
},
{
"number": "6076532",
"wms_qty": 1,
"item_id": "6076532"
}
]
我只向您展示我希望有效负载循环期间可用的安排在内存中的行为。
真诚的!
问题的逻辑并不完全清楚,但 DataWeave 中没有循环或迭代器,您可以“更新”上一次迭代的状态。您需要根据映射或归约操作来思考您的脚本。 Reduce 会将表达式应用于数组的每个项目,并让您更新最后一个表达式的状态,因此它是适合使用的函数。
您可以使用条件更新而不是 if 来简化条件。它还有助于更好地表达代码的“意图”。我还添加了 qty
的缺失条件。
%dw 2.0
var available = [
{
"number": "61327",
"qty": 1
},
{
"number": "6010550",
"qty": 1
},
{
"number": "6010482",
"qty": 1
},
{
"number": "6228245",
"qty": 1
},
{
"number": "6076532",
"qty": 1
}
]
fun updateValueFromAvailable(list, sku, qty) =
list
map ((item) ->
item update {
case .qty if(item.number == sku and item.qty <= qty) -> item.qty - qty
}
)
output application/json
---
payload reduce ((item, accumulator = available) ->
updateValueFromAvailable(accumulator, item.number, item.qty)
)
输出
[
{
"number": "61327",
"qty": 0
},
{
"number": "6010550",
"qty": 0
},
{
"number": "6010482",
"qty": 0
},
{
"number": "6228245",
"qty": 1
},
{
"number": "6076532",
"qty": 1
}
]
作为旁注,请尽可能使用键的全名。在代码中,清晰度和意图比少输入一个字母更有价值。
另请注意,如果不需要使用map()或mapObject()的可选参数,则可以省略它们。