gpt4 book ai didi

javascript - JSONata 获取父元素

转载 作者:行者123 更新时间:2023-11-29 17:36:27 25 4
gpt4 key购买 nike

在一个相当复杂的 JSON 对象中,我试图获得一个具有父值的键。

{
"provinces": [
{
"short": "ska",
"tiletype": "water",
"provinceOutlinePath": "M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z",
"unionParts": [
{
"id": "main",
"unionPartPath": "M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z"
},
{
"id": "main",
"unionPartPath": "M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z"
}
]
},
{
"short": "nws",
"tiletype": "water",
"provinceOutlinePath": "M140.038,0.667l34.86,68.197l-6.338,27.888l12.677,3.169l70.988,-17.747l100.144,-62.115l1.268,-19.392l-213.599,0Z",
"unionParts": [
{
"id": "main",
"unionPartPath": "M140.038,0.667l34.86,68.197l-6.338,27.888l12.677,3.169l70.988,-17.747l100.144,-62.115l1.268,-19.392l-213.599,0Z"
}
]
}
]
}

我想将对象更改为:

[
{
"short": ska,
"unionPartPath": "<Path>"
},
{
"short": ska,
"unionPartPath": "<AnotherPath>"
},
{
"short": nws,
"unionPartPath": "<Path>"
}
]

我已经浏览了整个文档,但没有找到像 .parent() 方法那样的东西。
也许使用一些高阶函数可以实现预期的结果,但目前我不知道如何实现它。

最佳答案

要在 JSONata 中执行此操作,您需要以下表达式

provinces.($short := short; unionParts.{
'short': $short,
'unionPartPath': unionPartPath
})

参见 http://try.jsonata.org/H1goy6AjE

关于javascript - JSONata 获取父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55976740/

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