Dataweave 2.0如何获取某个Key匹配的所有值?

How to get all values where a certain Key matches in Dataweave 2.0?

有效负载:

[
{
    "Contacts": "123456,098765",
    "Emails" : ""
},
{
    "Contacts": "ABC123",
    "Emails" : ""
}
]

如何从以下对象数组中获取所有电子邮件的列表,其中联系人 ID 与负载中的每一行相匹配? (下面的预期输出)

变量accConts

{
    "queryResponse": [
        {
            "Email": "test123@test.com",
            "SalesforceId": "123456"
        },
        {
            "Email": "test@test.com",
            "SalesforceId": "098765"
            
        },
        {
            "Email": "ABC@test.com",
            "SalesforceId": "ABC123"
            
        }

    ]
}

预期输出:

[
{
    "Contacts": "123456,098765",
    "Emails" : "test123@test.com, test@test.com"
},
{
    "Contacts": "ABC123",
    "Emails" : "ABC@test.com"
}
]

HTH..

%dw 2.0
output application/json



var qResp ={
    "queryResponse": [
        {
            "Email": "test123@test.com",
            "SalesforceId": "123456"
        },
        {
            "Email": "test@test.com",
            "SalesforceId": "098765"
            
        },
        {
            "Email": "ABC@test.com",
            "SalesforceId": "ABC123"
            
        }

    ]
}


--- 


payload filter ($.Contacts != null) map using (iter = $$) {
    "Contacts" : $.Contacts,
    "Emails": (qResp.queryResponse filter (payload[iter].Contacts contains $.SalesforceId)) reduce ((item,acc = "") ->  (acc ++ "," ++ item.Email)[1 to -1]
    )

}


我接受了 Salim Khan 的回答,因为他指导了我正确的方向和使电子邮件正常工作的逻辑。我只需要修改地图逻辑,

payload map (row, index) -> {
    "Contacts" : row."Contacts",
    "Emails" : (qResp.queryResponse filter (row."Contacts" contains $.SalesforceId)) reduce ((item,acc = "") ->  (acc ++ "," ++ item.Email)[1 to -1]
    ),
}

希望这个比较对您有所帮助

想要添加一个稍微更简洁的解决方案来展示另一种方法。

%dw 2.0
output application/json

var qResp =
{
    "queryResponse": [
        {
            "Email": "test123@test.com",
            "SalesforceId": "123456"
        },
        {
            "Email": "test@test.com",
            "SalesforceId": "098765"
            
        },
        {
            "Email": "ABC@test.com",
            "SalesforceId": "ABC123"
            
        }

    ]
}

--- 

payload map (value) ->
    {
        'Contacts':value.Contacts,
        'Emails': qResp.queryResponse[?(value.Contacts contains $.SalesforceId)]..Email joinBy ", "
    }