在一个相当复杂的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()
方法。
也许可以通过一些高阶函数来达到预期的效果,但是目前我不知道如何实现。
答案 0 :(得分:1)
您可以尝试这样的事情。
在这里,我使用了在数组上定义的 map()方法。您请确保使用正确的路径。
正如您提到的
<yourPath>
,<Path>
一样,我刚刚指定了<AnotherPath>
(您知道这是什么意思,所以只需替换即可)
将对象分配给任何变量,例如到 obj 。现在我们可以使用这1行语句获取结果
result = obj.provinces.map((obj2) => obj2.unionParts.map((obj3) => {return {"short": obj2.short, "unionPartPath": "<yourPath>"}}))
初始化»
> 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>' } ] ]
>
漂亮地打印对象»
> console.log(JSON.stringify(result, null, 4)) // Pretty print of object
[
[
{
"short": "ska",
"unionPartPath": "<yourPath>"
},
{
"short": "ska",
"unionPartPath": "<yourPath>"
}
],
[
{
"short": "nws",
"unionPartPath": "<yourPath>"
}
]
]
undefined
>
答案 1 :(得分:1)
一个简单的for-of
循环应该可以做到:
const jsonData = {
"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"
}]
}
]
};
const result = [];
for (let p of jsonData.provinces) {
for (let part of p.unionParts) {
result.push({short: p.short, unionPartPath: part.unionPartPath});
}
}
console.log(result);
答案 2 :(得分:1)
要在JSONata中执行此操作,您需要以下表达式
provinces.($short := short; unionParts.{
'short': $short,
'unionPartPath': unionPartPath
})
答案 3 :(得分:0)
以下是将Array.reduce()与destructuring功能结合使用的一种解决方案:
const jsonData = {"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"}]}]};
let res = jsonData.provinces.reduce((acc, {short, unionParts}) =>
{
unionParts.forEach(({unionPartPath}) => acc.push({short, unionPartPath}));
return acc;
}, []);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}