特定点的 JSONata 字符串操作

发布于 2025-01-09 14:57:03 字数 803 浏览 2 评论 0原文

给定这样的数组,

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-01-16 14:57:03

您可以使用 $reduce 将数组转换为字符串。归约操作连接字符串。诀窍是不仅要减少为字符串,还要减少为包含长度计数器和连接结果的对象:

{
  "count": chars since the last ;
  "result": the concatenated string
}

以下是表达式:

(
    /* initial reduce value: count 0 with empty string */
    $init := {
        "count": 0,
        "result": ""
    };
    /* $ is the input array */
    $reduce($, function($i, $j){
        /* $i is the count / result object, $j is the current array string */ 
        $i.count + 1 + $length($j) >= 30 ?
            /* too long, use ; and reset the length */
            {
                "count": $length($j),
                "result": $i.result & ";" & $j
            }
        :
            $i.count = 0 ? 
                /* happens only in the very beginning, avoid leading , */
                {
                    "count": $length($j),
                    "result": $j
                }
            :
                /* append using , */
                {
                    "count": $i.count + 1 + $length($j),
                    "result": $i.result & "," & $j
                }
    }, $init);
).result

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:

{
  "count": chars since the last ;
  "result": the concatenated string
}

Here's the expression:

(
    /* initial reduce value: count 0 with empty string */
    $init := {
        "count": 0,
        "result": ""
    };
    /* $ is the input array */
    $reduce($, function($i, $j){
        /* $i is the count / result object, $j is the current array string */ 
        $i.count + 1 + $length($j) >= 30 ?
            /* too long, use ; and reset the length */
            {
                "count": $length($j),
                "result": $i.result & ";" & $j
            }
        :
            $i.count = 0 ? 
                /* happens only in the very beginning, avoid leading , */
                {
                    "count": $length($j),
                    "result": $j
                }
            :
                /* append using , */
                {
                    "count": $i.count + 1 + $length($j),
                    "result": $i.result & "," & $j
                }
    }, $init);
).result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文