如果参数是数组值的元素,则需要将键添加到现有数组。
%dw 2.0
var myArray = [];
var subjects = {
"subject1" = ["sectionA", "sectionB", "sectionC"],
"subject2" = ["sectionB", "sectionD", "sectionE"],
"subject3" = ["sectionC", "sectionF", "sectionG"],
}
fun getSubjects(section) = mapping mapObject ((value, key, index) ->
if(value contains key) myArray << key
else myArray
)
---
{
"subjects" : getSubjects(var.section) // var.section = sectionA
}
But I'm getting this error "Cannot coerce Array to Object". Is there a better way to do this?
我认为这就是您想要做的;因为数据在dataweave中是不可变的,所以如果要在更改数组内容等内容的同时迭代集合,则必须使用诸如reduce之类的方法。
%dw 2.0
var subjects = {
"subject1": ["sectionA", "sectionB", "sectionC"],
"subject2": ["sectionB", "sectionD", "sectionE"],
"subject3": ["sectionC", "sectionF", "sectionG"]
}
var section = "sectionA"
fun getSubjects(section) = subjects pluck $$ reduce ((subject, myArray=[]) ->
if (subjects[subject] contains section) myArray << subject
else myArray
)
---
{
subjects: getSubjects(section)
}
产量:
{
subjects: [
"subject1"
]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句