【发布时间】:2021-04-22 16:41:27
【问题描述】:
我有 2 个数组存储在一个变量中
vars.array1
[
{
"id": 123,
"name": "Sam",
"class": "first"
},
{
"id": 124,
"name": "John",
"class": "first"
},
{
"id": 125,
"name": "Max",
"class": "second"
}
]
vars.array2
[
{
"studentId": 123,
"course": "science",
"otherActivities": "sports"
},
{
"studentId": 125,
"course": "arts",
"otherActivities": "studentCouncil"
},
{
"studentId": 126,
"course": "literature",
"otherActivities": "drama"
}
]
预期输出:
[
{
"id": 123,
"name": "Sam",
"class": "first",
"course": "science",
"otherActivities": "sports"
},
{
"id": 125,
"name": "Max",
"class": "second",
"course": "arts",
"otherActivities": "studentCouncil"
}
]
原始数组包含大约 5000 个。
现在,我正在使用它作为解决方案,
//this code is inside 'for each' with collection as vars.array1
//vars.array3 is inilialised as [] before 'for each'
%dw 2.0
output application/json
---
if(sizeOf(vars.array2 filter $["studentId"] == payload.id) > 0)
(vars.array3 << (payload ++ (vars.array2 filter $["studentId"] == payload.id)[0]))
else vars.array3
这适用于大约 500 条记录,但需要 5k 条记录。想知道是否有任何其他方法可以降低复杂性,从而加快响应速度。
【问题讨论】: