我有一张代表有间隙数组的地图;键是数组的索引:
let $m := map{1: "val1", 2: "val2", 5: "val5", 7: "val7"} return
现在我想将其转换为真正的数组,用
null
填充缺失的索引:
array {
( 1 to max($m => map:keys()) ) ! (
if ($m => map:contains(.)) then
$m(.)
else
null(())
)
}
但我明白:
["val1", "val2", "val5", "val7"]
而不是我的预期输出:
["val1", "val2", null, null, "val5", null, "val7"]
一个可能的解决方案是生成一系列
array(item()?)
并在其上使用 array:join
:
let $m := map{1: "1", 2: "2", 5: "5", 7: "7"} return
array:join( (1 to max(map:keys($m))) ! [$m(.)] )
["1", "2", null, null, "5", null, "7"]