How can I merge the following two lists ids
and attributes
based on the matching did
field and for each matched pair increment the iterid
values
For example I have two lists below. size of the list differ dynamically
ids
[
{
"id": "d3c0da07-e58f4606bm",
"did": "714c9836927d"
}
]
attributes
[
{
"snm": "3298SJDJ",
"did": "714c9836927d"
}
]
I want to achieve the result like below
[
{
"iterid": 1,
"id": "d3c0da07-e58f4606bm",
"did": "714c9836927d"
},
{
"iterid": 2,
"id": "d3c0da07-e58f4606bm",
"did": "714c9836927d"
}
]
I've tried with flatMap
to form the merged list, but I'm being blocked at incrementing the iterid
count dynamically
You can do it with same flatMap
along with adding a logic to increment a number by leveraging the index of the elements in each iteration.
Here is the example below
%dw 2.0
output application/json
var ids = [
{
"id": "d3c0da07-e58f4606bm",
"did": "714c9836927d"
}
]
var attributes = [
{
"snm": "3298SJDJ",
"did": "714c9836927d"
}
]
var data = ids flatMap ((id) ->
(attributes filter ((attribute) -> attribute.did == id.did))
map ((attribute) -> {
snm: attribute.snm,
did: id.did,
id: id.id
})
)
---
data flatMap ((item, index) -> [
{
iterid: (index * 2) + 1,
id: item.id,
did: item.did
},
{
iterid: (index * 2) + 2,
id: item.id,
did: item.did
}
])