gpt4 book ai didi

javascript - 使用 jq 将元素数组分解为多个不同长度的数组

转载 作者:行者123 更新时间:2023-12-03 06:01:20 24 4
gpt4 key购买 nike

我有一个 JSON 对象,我想使用 jq 将其从一种形式转换为另一种形式(当然,我可以使用 javascript 或 python 进行迭代,但 jq 会更好)。问题在于,输入包含长数组,每当数据在第一个数组中停止重复时,就需要将其分解为多个较小的数组。我不太确定如何描述这个问题,所以我只是在这里举一个例子,希望能更具解释性。一个安全的假设(如果有任何帮助的话)是输入数据始终在前两个元素上进行预排序(例如“row_x”和“col_y”):

输入:

{
"headers": [ "col1", "col2", "col3" ],
"data": [
[ "row1","col1","b","src2" ],
[ "row1","col1","b","src1" ],
[ "row1","col1","b","src3" ],
[ "row1","col2","d","src4" ],
[ "row1","col2","e","src5" ],
[ "row1","col2","f","src6" ],
[ "row1","col3","j","src7" ],
[ "row1","col3","g","src8" ],
[ "row1","col3","h","src9" ],
[ "row1","col3","i","src10" ],
[ "row2","col1","l","src13" ],
[ "row2","col1","j","src11" ],
[ "row2","col1","k","src12" ],
[ "row2","col3","o","src15" ]
]
}

期望的输出:

{
"headers": [ "col1", "col2", "col3" ],
"values": [
[["b","b","b"],["d","e","f"],["g","h","i","j"]],
[["j","k","l"],null,["o"]]
],
"sources": [
[["src1","src2","src3"],["src4","src5","src6"],["src7","src8","src9","src10"]],
[["src11","src12","src13"],null,["src15"]]
]
}

这在 jq 中可行吗?

更新:它的一个变体是保留原始数据顺序,因此输出如下:

{
"headers": [ "col1", "col2", "col3" ],
"values": [
[["b","b","b"],["d","e","f"],["j","g","h","i"]],
[["l","j","k"],null,["o"]]
],
"sources": [
[["src2","src1","src3"],["src4","src5","src6"],["src7","src8","src9","src10"]],
[["src13","src11","src12"],null,["src15"]]
]
}

最佳答案

可行吗?当然!

首先,您需要按行然后按列对数据进行分组。然后通过组构建您的值/源数组。

.headers as $headers | .data
# make the data easier to access
| map({ row: .[0], col: .[1], val: .[2], src: .[3] })
# keep it sorted so they are in expected order in the end
| sort_by([.row,.col,.src])
# group by rows
| group_by(.row)
# create a map to each of the cols for easier access
| map(group_by(.col)
| reduce .[] as $col ({};
.[$col[0].col] = [$col[] | {val,src}]
)
)
# build the result
| {
headers: $headers,
values: map([.[$headers[]] | [.[]?.val]]),
sources: map([.[$headers[]] | [.[]?.src]])
}

这将产生以下结果:

{
"headers": [ "col1", "col2", "col3" ],
"values": [
[
[ "b", "b", "b" ],
[ "d", "e", "f" ],
[ "i", "j", "g", "h" ]
],
[
[ "j", "k", "l" ],
[],
[ "o" ]
]
],
"sources": [
[
[ "src1", "src2", "src3" ],
[ "src4", "src5", "src6" ],
[ "src10", "src7", "src8", "src9" ]
],
[
[ "src11", "src12", "src13" ],
[],
[ "src15" ]
]
]
}

关于javascript - 使用 jq 将元素数组分解为多个不同长度的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39739195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com