Javascript JSONata获取父元素
在一个相当复杂的JSON对象中,我试图获取一个具有父值的键Javascript JSONata获取父元素,javascript,traversal,jsonata,Javascript,Traversal,Jsonata,在一个相当复杂的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.7
{
"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>"
}
]
[
{
“短”:斯卡,
“unionPartPath”:”
},
{
“短”:斯卡,
“unionPartPath”:”
},
{
“短”:nws,
“unionPartPath”:”
}
]
我已经浏览了整个文档,没有找到类似于.parent()
方法的内容。也许使用一些高阶函数可以达到预期的效果,但目前我不知道如何实现这一点。您可以尝试类似的方法 这里我使用了数组上定义的map()方法。请确保使用正确的路径 我刚刚指定了
,正如您所提到的
,
(您很清楚这意味着什么,所以只需替换)
将对象指定给任何变量,例如toobj。现在我们可以使用这一行语句来得到结果
result=obj.provinces.map((obj2)=>obj2.unionParts.map((obj3)=>{return{“short”:obj2.short,“unionPartPath”:“}}”)
初始化»
> let obj = {
... "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"
....... }
..... ]
..... }
... ]
... }
undefined
>
最后»
> result = obj.provinces.map((obj2) => obj2.unionParts.map((obj3) => {return {"short": obj2.short, "unionPartPath": "<yourPath>"}}))
[ [ { short: 'ska', unionPartPath: '<yourPath>' },
{ short: 'ska', unionPartPath: '<yourPath>' } ],
[ { short: 'nws', unionPartPath: '<yourPath>' } ] ]
>
>result=obj.provinces.map((obj2)=>obj2.unionParts.map((obj3)=>{return{“short”:obj2.short,“unionPartPath”:“}}”)
[[{short:'ska',unionPartPath:'},
{short:'ska',unionPartPath:'}],
[{short:'nws',unionPartPath:'}]]
>
漂亮的打印对象»
> console.log(JSON.stringify(result, null, 4)) // Pretty print of object
[
[
{
"short": "ska",
"unionPartPath": "<yourPath>"
},
{
"short": "ska",
"unionPartPath": "<yourPath>"
}
],
[
{
"short": "nws",
"unionPartPath": "<yourPath>"
}
]
]
undefined
>
>console.log(JSON.stringify(result,null,4))//对象的漂亮打印
[
[
{
“短”:“ska”,
“unionPartPath”:”
},
{
“短”:“ska”,
“unionPartPath”:”
}
],
[
{
“短”:“nws”,
“unionPartPath”:”
}
]
]
未定义
>
一个简单的for of
循环应该可以:
const jsonData={
“省”:[{
“短”:“ska”,
“tiletype”:“水”,
“省大纲路径”:“M255.848145.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.848145.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.848145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z”
}
]
},
{
“短”:“nws”,
“tiletype”:“水”,
“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”,
“部分”:[{
“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”
}]
}
]
};
常量结果=[];
对于(让p代表jsonData省){
对于(让部分p.unionParts){
push({short:p.short,unionPartPath:part.unionPartPath});
}
}
控制台日志(结果)代码>这里有一个与功能结合使用的解决方案:
(代码>代码>cons>cons>cons>cons>cons>cons>cons.json.json.json.json.jsn.concons.jss.com>cons.jsn.com.json.json.json.json数据={“省:”{“省:“以下以下”省::“[省::“[省::::“水”,“水”,“省”水”,“水”,“省”水”,“省”(水”,“省”(水”,“省:::::55.8455.848145.848145.848145.8145.8145.8145.8145.819.4119.4119.8119.839.4119.839.839.8119.839.839.839.4119.4119.8119.839.839,9,0.839,0.839,0.839,0.839,0.9,0.9,0.9{“id”:“main”,7.8.6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7路径“:”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“}];
让res=jsonData.provinces.reduce((acc,{short,unionParts})=>
{
forEach({unionPartPath})=>acc.push({short,unionPartPath}));
返回acc;
}, []);
console.log(res);
.as控制台{背景色:黑色!重要;颜色:石灰;}
.作为控制台包装器{max height:100%!important;top:0;}
要在JSONata中执行此操作,需要以下表达式
provinces.($short := short; unionParts.{
'short': $short,
'unionPartPath': unionPartPath
})
请参见您不需要获取具有父值的键,只需循环遍历省
数组中的所有项,然后循环遍历unionParts
数组中的所有项,同时在顶部循环的当前迭代中保留对该项的short
值的引用。