【发布时间】:2022-10-07 14:03:40
【问题描述】:
如何使用存储在字典中的类别扩展数组?
例如。对于以下输入,
{\"weigths\":
[
{\"name\":\"apple\",\"weigth\":200},
{\"name\":\"tomato\", \"weigth\":100}
],
\"categories\":
[
{\"name\":\"apple\",\"category\":\"fruit\"},
{\"name\":\"tomato\",\"category\":\"vegetable\"}
]
}
我需要一种高效的方法将各自的category 附加到weights 中的每个对象,从而产生输出:
{\"weigths\":
[
{\"name\":\"apple\",\"weigth\":200, \"category\": \"fruit\"},
{\"name\":\"tomato\", \"weigth\":100, \"category\": \"vegetable\"}
],
}
是给JOIN/4 的吗? (我从未尝试过)
更新:
理想情况下,我想处理类别的非 SQL 对象:完整输入看起来像这样
{\"weigths\":
[
{\"name\":\"apple\",\"weigth\":200},
{\"name\":\"orange\", \"weigth\":300}
{\"name\":\"tomato\",\"weigth\":100},
{\"name\":\"spinach\",\"weigth\":50},
],
\"categories\":
{
\"fruit\": [\"apple\", \"orange\"],
\"vegetable\": [\"tomato\", \"spinach\"]
}
}
...仍然得到类似的输出:
{\"weigths\":
[
{\"name\": \"apple\", \"weigth\": 200, \"category\": \"fruit\"},
{\"name\": \"orange\", \"weigth\": 300, \"category\": \"fruit\"},
{\"name\": \"tomato\", \"weigth\": 100, \"category\": \"vegetable\"}
{\"name\": \"spinach\", \"weigth\": 50, \"category\": \"vegetable\"}
],
}
标签: jq