gpt4 book ai didi

dataweave - 骡子 4 : Dataweave 2. 0 : is there any way we can have a filter condition in groupBy method that works similar to SQL GroupBY and Having?

转载 作者:行者123 更新时间:2023-12-04 01:09:54 25 4
gpt4 key购买 nike

场景:给定如下输入 sampleArray ,我想将所有有特定老师的学生分组。

在 DataWeave 中,我们有一个方法 groupBy 允许我们对指定字符串键的数组进行分组。但是这里因为 item.studentsMarks.subjectTeacher 返回一个数组,所以我收到下面指定的错误。

谁能帮忙。提前致谢。

下面附上预期输出。

示例输入:

var sampleArray = [
{
"studentName" : "ABC",
"studentsMarks" : [
{
"subject" : "maths",
"marks" : "50",
"subjectTeacher" : "teacher_1"
},
{
"subject" : "science",
"marks" : "30",
"subjectTeacher" : "teacher_3"
}
]
},
{
"studentName" : "XYZ",
"studentsMarks" : [
{
"subject" : "maths",
"marks" : "90",
"subjectTeacher" : "teacher_1"

},
{
"subject" : "arts",
"marks" : "50",
"subjectTeacher" : "teacher_2"
}
]
}
]


尝试过的代码:

payload groupBy ((item, index) -> item.studentsMarks.subjectTeacher)

错误:

Cannot coerce Array to String

预期输出:

[

"teacher_1" : [{
"studentName" : "ABC",
"studentsMarks" : [
{
"subject" : "maths",
"marks" : "50",
"subjectTeacher" : "teacher_1"
},
{
"subject" : "science",
"marks" : "30",
"subjectTeacher" : "teacher_3"
}
]
},
{
"studentName" : "XYZ",
"studentsMarks" : [
{
"subject" : "maths",
"marks" : "90",
"subjectTeacher" : "teacher_1"

},
{
"subject" : "arts",
"marks" : "50",
"subjectTeacher" : "teacher_2"
}
]
}
],
"teacher_2" : [

{
"studentName" : "XYZ",
"studentsMarks" : [
{
"subject" : "maths",
"marks" : "90",
"subjectTeacher" : "teacher_1"

},
{
"subject" : "arts",
"marks" : "50",
"subjectTeacher" : "teacher_2"
}
]
}
],
"teacher_3" : [

{
"studentName" : "ABC",
"studentsMarks" : [
{
"subject" : "maths",
"marks" : "50",
"subjectTeacher" : "teacher_1"
},
{
"subject" : "science",
"marks" : "30",
"subjectTeacher" : "teacher_3"
}
]
}
]

]

最佳答案

这里有一个非常快速的方法来完成它。不过,它包含的不仅仅是 groupBy:

%dw 2.0
output application/json

var sampleArray = [
{
"studentName" : "ABC",
"studentsMarks" : [
{
"subject" : "maths",
"marks" : "50",
"subjectTeacher" : "teacher_1"
},
{
"subject" : "science",
"marks" : "30",
"subjectTeacher" : "teacher_3"
}
]
},
{
"studentName" : "XYZ",
"studentsMarks" : [
{
"subject" : "maths",
"marks" : "90",
"subjectTeacher" : "teacher_1"

},
{
"subject" : "arts",
"marks" : "50",
"subjectTeacher" : "teacher_2"
}
]
}
]
---
// Get list of all teachers
sampleArray..*subjectTeacher
// Change the collection to a set
distinctBy $
// Iterate over every single teacher and create an object where
// the field is the teacher while the value is a collection
// containing all students where they attend a class for the teacher.
map {
($): (sampleArray dw::core::Arrays::partition (e) -> e..*subjectTeacher contains $).success
}

关于dataweave - 骡子 4 : Dataweave 2. 0 : is there any way we can have a filter condition in groupBy method that works similar to SQL GroupBY and Having?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65201950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com