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

问题描述

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

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 ("")

enter image description here

解决方法

问题在于您如何调用 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 ("")