如果使用 Dataweave 存在元素,则将键添加到数组
Add Key to an Array if Element Exist using Dataweave
我有一个 JSON 对象,它包含 subject1、subject2、subject3 等字段,并以字符串数组作为值。
如果分配给字段 subject1、subject2、subject3 的字符串数组中存在任何搜索元素,那么我需要将 Json 对象中的键或该字段添加到现有数组 (myArray),
%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
}
但我收到此错误“无法将数组强制转换为对象”。有更好的方法吗?
我认为这就是你想要做的;因为数据在 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"
]
}
还有一种方法可以做到这一点,如下所示,我们发送变量和需要匹配的字符串
%dw 2.0
var myArray = []
var subjects = {
"subject1" : ["sectionA", "sectionB", "sectionC"],
"subject2" : ["sectionA", "sectionD", "sectionE"],
"subject3" : ["sectionC", "sectionF", "sectionG"],
}
fun adding(inputarray,matchingkey)= flatten(inputarray mapObject ((value, key) ->
(("myArray"): (myArray ++ [key])) if ((value) contains matchingkey)
) pluck $)
---
adding(subjects,"sectionC")
输出为:
[
"subject1",
"subject3"
]
我有一个 JSON 对象,它包含 subject1、subject2、subject3 等字段,并以字符串数组作为值。
如果分配给字段 subject1、subject2、subject3 的字符串数组中存在任何搜索元素,那么我需要将 Json 对象中的键或该字段添加到现有数组 (myArray),
%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
}
但我收到此错误“无法将数组强制转换为对象”。有更好的方法吗?
我认为这就是你想要做的;因为数据在 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"
]
}
还有一种方法可以做到这一点,如下所示,我们发送变量和需要匹配的字符串
%dw 2.0
var myArray = []
var subjects = {
"subject1" : ["sectionA", "sectionB", "sectionC"],
"subject2" : ["sectionA", "sectionD", "sectionE"],
"subject3" : ["sectionC", "sectionF", "sectionG"],
}
fun adding(inputarray,matchingkey)= flatten(inputarray mapObject ((value, key) ->
(("myArray"): (myArray ++ [key])) if ((value) contains matchingkey)
) pluck $)
---
adding(subjects,"sectionC")
输出为:
[
"subject1",
"subject3"
]