如何在数据编织中拆分字符串后从数组中获取第一个值?

How to get the first value from an Array after split String in Data weave?

我正在尝试比较拆分字符串后字符串数组中的第一个值。 但是我的子函数抛出错误,说它接收到一个数组,我做错了什么。

fun getErrorList() =
compareAddress(loanData.loan.propertyAddress,payload.propertyAddress)
fun compareAddress(loanpropertyAddress, inputPropertyAddress) =
if(null != loanpropertyAddress and null != inputPropertyAddress)
getAddr1 (loanpropertyAddress splitBy(" "))[0]
==
getAddr1 (inputPropertyAddress splitBy(" "))[0]
else
null

fun getAddr1(addr1) =
addr1 replace "#" with ("")

问题在于您如何调用 getAddr1 函数。在您提供的 DataWeave 表达式中,您将字符串数组而不是字符串传递给 getAddr1 函数:

...
getAddr1(
    loadpropertyAddress splitBy(" ") // splitBy returns an array of strings
)[0] // here, you're trying to get the first element of the value returned by getAddr1
...

我假设您正在尝试比较贷款的第一部分和删除“#”字符后的输入 属性 地址。如果我的理解是正确的,那么你可以对你的函数做如下修改:

...
getAddr1(
    loadpropertyAddress splitBy(" ")[0] // get the first element of the string array returned by the splitBy function
) // removed array item selector ([0])
...

经过该修改,您的 DataWeave 表达式应如下所示:

fun getErrorList() =
compareAddress(loanData.loan.propertyAddress,payload.propertyAddress)
fun compareAddress(loanpropertyAddress, inputPropertyAddress) =
if(null != loanpropertyAddress and null != inputPropertyAddress)
getAddr1 (loanpropertyAddress splitBy(" ")[0])
==
getAddr1 (inputPropertyAddress splitBy(" ")[0])
else
null

fun getAddr1(addr1) =
addr1 replace "#" with ("")