阵列1:
[
{
"name": "Abc",
"address":"Mumbai",
"phone":"91-1234567891",
"id":123
},
{
"name": "Pqr",
"address":"Pune",
"phone":"91-1234985438",
"id":456
},
{
"name": "Abc",
"address":"Delhi",
"phone":"91-1234567891",
"id":123
}
]
数组2:
[
{
"sid":123,
"postalCode": "67890",
"country": "USA",
"sname": "tyu"
},
{
"sid":864,
"postalCode": "54983",
"country": "UK",
"sname": "Pqr"
},
{
"sid":123,
"postalCode": "27932",
"country": "EU",
"sname": "Uvg"
}
]
上面我们必须考虑 ID 和名称,如果其中任何一个字段与 leftjoin 匹配,那么我们就必须考虑它们。
预期输出:
[
{
"name": "Abc",
"address": "Mumbai",
"phone": "91-1234567891",
"id": 123,
"sid": 123,
"sname": "tyu",
"postalCode": "27932",
"country": "EU"
},
{
"name": "Pqr",
"address": "Pune",
"phone": "91-1234985438",
"id": 456,
"sid": 864,
"sname": "Pqr",
"postalCode": "54983",
"country": "UK"
},
{
"name": "Abc",
"address": "Delhi",
"phone": "91-1234567891",
"id": 123,
"sid": 123,
"sname": "Uvg",
"postalCode": "27932",
"country": "EU"
}
]
数据编织:
%dw 2.0
output application/json
import * from dw::core::Arrays
var Array2reversed=Array2[-1 to 0] distinctBy ((item, index) -> item.sid)
var combine=leftJoin(Array1, Array2reversed, (Array1) -> Array1.name, (Array2) -> Array2.sname) map $
---
(leftJoin(combine, Array2reversed, (Array1) -> Array1.id, (Array2) -> Array2.sid))
map {
"name": $.l.name,
"address": $.l.address,
"phone": $.l.phone,
"id": $.l.id,
"sid": $.r.sid,
"postalCode": $.r.postalCode,
"country": $.r.country
}.
我们必须将 name 和 sname 视为左连接中的另一个条件。我试图放弃函数,但出现错误,并且无法仅在一个连接中加入多个条件。
你可以试试这个。
%dw 2.0
var Array1 = [
{
"name": "Abc",
"address":"Mumbai",
"phone":"91-1234567891",
"id":123
},
{
"name": "Pqr",
"address":"Pune",
"phone":"91-1234985438",
"id":456
},
{
"name": "Abc",
"address":"Delhi",
"phone":"91-1234567891",
"id":123
}
]
var Array2 = [
{
"sid":123,
"postalCode": "67890",
"country": "USA",
"sname": "tyu"
},
{
"sid":864,
"postalCode": "54983",
"country": "UK",
"sname": "Pqr"
},
{
"sid":123,
"postalCode": "27932",
"country": "EU",
"sname": "Uvg"
}
]
output application/json
import * from dw::core::Arrays
var Array2reversed=Array2[-1 to 0] distinctBy ((item, index) -> item.sid)
var combine = leftJoin(Array1, Array2reversed, (Array1) -> Array1.name, (Array2) -> Array2.sname) map ((item, index) -> item.l ++ (item.r default {}))
---
(leftJoin(combine, Array2reversed, (Array1) -> Array1.id, (Array2) -> Array2.sid))
map {
"name": $.l.name,
"address": $.l.address,
"phone": $.l.phone,
"id": $.l.id,
"sid": $.r.sid default $.l.sid,
"sname": $.r.sname default $.l.sname,
"postalCode": $.r.postalCode default $.l.postalCode,
"country": $.r.country default $.l.country
}
输出
[
{
"name": "Abc",
"address": "Mumbai",
"phone": "91-1234567891",
"id": 123,
"sid": 123,
"sname": "Uvg",
"postalCode": "27932",
"country": "EU"
},
{
"name": "Pqr",
"address": "Pune",
"phone": "91-1234985438",
"id": 456,
"sid": 864,
"sname": "Pqr",
"postalCode": "54983",
"country": "UK"
},
{
"name": "Abc",
"address": "Delhi",
"phone": "91-1234567891",
"id": 123,
"sid": 123,
"sname": "Uvg",
"postalCode": "27932",
"country": "EU"
}
]