特定点的 JSONata 字符串操作
给定这样的数组,
$s := [
"aString 1",
"bString",
"test",
"test 2",
"window 1",
"window 2",
"Garage",
"Sport",
"cString",
"etc"
]
我需要一个带有逗号分隔数组元素的字符串。
但是,子字符串的总长度不得超过 30。在这些点上,必须使用分号来代替逗号。
通过一个简单的操作,
$s ~> $join(',')
我得到
"aString 1,bString,test,test 2,window 1,window 2,Garage,Sport,cString,etc"
这里第 4 个和第 7 个逗号必须用分号替换,因为否则这些部分将 >= 30 像这样
"aString 1,bString,test,test 2;window 1,window 2,Garage;Sport,cString,etc"
JSONata 字符串函数是否有可能?
我尝试过使用这样的 $map() 函数:
(
$result := "";
$map($s , function($v){
$result := $result & $v & ($length($result) < 30 ? ',' : ';')
});
)
但这不起作用。 $result 的操作仅在函数内有效。 我怎么能这么做呢?
Given array like this
$s := [
"aString 1",
"bString",
"test",
"test 2",
"window 1",
"window 2",
"Garage",
"Sport",
"cString",
"etc"
]
I need a string with the comma-separated array elements.
However, the total length of the substring must not exceed 30. At these points, instead of the comma, a semicolon must be used.
With a simple
$s ~> $join(',')
i get
"aString 1,bString,test,test 2,window 1,window 2,Garage,Sport,cString,etc"
Here the 4th and the 7th comma must be replaced by a semicolon, because otherwise the sections are >= 30 like so
"aString 1,bString,test,test 2;window 1,window 2,Garage;Sport,cString,etc"
Is there a possibility with the JSONata string functions?
I have tried with a $map() function like this:
(
$result := "";
$map($s , function($v){
$result := $result & $v & ($length($result) < 30 ? ',' : ';')
});
)
But this does not work. The manipulation of $result only works within the function.
How could i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 $reduce 将数组转换为字符串。归约操作连接字符串。诀窍是不仅要减少为字符串,还要减少为包含长度计数器和连接结果的对象:
以下是表达式:
You use $reduce in order to transform the array to a string. The reduce operation concatenates the string. The trick is to reduce not only to a string, but an object that contains a length counter and the concatenated result:
Here's the expression: