首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSONata获取父元素

JSONata获取父元素
EN

Stack Overflow用户
提问于 2019-05-03 20:32:55
回答 4查看 784关注 0票数 2

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

代码语言:javascript
复制
{
    "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"
                }
            ]
        }
   ]
}

我想将对象更改为:

代码语言:javascript
复制
[
  { 
    "short": ska,
    "unionPartPath": "<Path>"
  },
  { 
    "short": ska,
    "unionPartPath": "<AnotherPath>"
  },
  { 
    "short": nws,
    "unionPartPath": "<Path>"
  }
]

我已经浏览了整个文档,没有发现任何类似于.parent()方法的东西。

也许可以通过一些高阶函数来实现预期的结果,但目前我不知道如何实现这个功能。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-05-07 08:21:36

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

代码语言:javascript
复制
provinces.($short := short; unionParts.{
  'short': $short,
  'unionPartPath': unionPartPath
})

请参阅http://try.jsonata.org/H1goy6AjE

票数 2
EN

Stack Overflow用户

发布于 2019-05-03 20:50:06

你可以试试这样的东西。

这里我使用了定义在数组上的map()方法。请务必使用正确的路径。 我刚刚指定了<yourPath>,正如您提到的<Path><AnotherPath> (您很清楚这意味着什么,所以只需替换)

将对象赋值给任何变量,例如obj。现在我们可以使用以下1行语句来获得结果

result = obj.provinces.map((obj2) => obj2.unionParts.map((obj3) => {return {"short": obj2.short, "unionPartPath": "<yourPath>"}}))

初始化

代码语言:javascript
复制
> 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
> 

最后

代码语言:javascript
复制
> 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>' } ] ]
> 

漂亮的打印对象?

代码语言:javascript
复制
> 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
EN

Stack Overflow用户

发布于 2019-05-03 20:54:30

一个简单的for-of循环应该这样做:

代码语言:javascript
复制
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);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55976740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档