英文:
How can I map an array with a condition
问题
所以我需要映射一个dN如果mD为true。但我不确定如何添加条件来检查,到目前为止我最远的进展是映射所有的dN。
这是有效载荷:
[{
        "country" : "AT",
        "groupMembers": [
            {
                "dN": "268",
                
                "mD": true
            },
            {
                "dN": "240",
                
                "mD": false
            }
        ]
    },
    {
        "country" : "RO",
        "groupMembers": [
            {
                "dN": "273",
                
                "mD": true
            },
            {
                "dN": "292",
               
                "mD": false
            }
        ]
    }]
我所做的是这样的:
payload map (value,index) -> {  country : value.country,
LC: value.groupMembers.dN }
并得到了以下输出:
[{
    "country" : "at",
    "LC": [
      "268",
      "240"
    ]
  },
  {
    "country" : "pt",
    "LC": [
      "273",
      "292"
    ]
  }]
我如何才能让只有"268"和"273"被映射,因为"MD"中的true语句?
预期的结果应该类似于:
[{
  "country" : "at",
  "LC": "268"
},
{
  "country" : "pt",
  "LC": "273"
}]
英文:
So I need to map a dN if the mD is true.
But I am not sure on how do I add the condition in order to check, the furthest I got was mapping all the dN's.
This is the payload:
[{
        "country" : "AT",
        "groupMembers": [
            {
                "dN": "268",
                
                "mD": true
            },
            {
                "dN": "240",
                
                "mD": false
            }
        ]
    },
    {
        "country" : "RO",
        "groupMembers": [
            {
                "dN": "273",
                
                "mD": true
            },
            {
                "dN": "292",
               
                "mD": false
            }
        ]
    }]
What I did was this:
payload map (value,index) -> {  country : value.country,
LC: value.groupMembers.dN }
And got this Output:
[
  { 
    "country" : "at",
    "LC": [
      "268",
      "240"
    ]
  },
  {
    "country" : "pt",
    "LC": [
      "273",
      "292"
    ]
  }]
How can I make it so that only "268" and "273" get mapped because of the true statement in "MD"?
The expected outcome should be something like :
[
  { "country" : "at",
    "LC": "268"
  },
  { "country" : "pt",
    "LC": "273"
  }
]
答案1
得分: 0
以下脚本将帮助您。
%dw 2.0
output application/json
---
LC : (payload.groupMembers flatMap ($ filter($.mD == true))) map (
    ($.dN)
)
英文:
Below script will help you.
%dw 2.0
output application/json
---
LC : (payload.groupMembers flatMap ($ filter($.mD == true))) map (
    ($.dN)
)
答案2
得分: 0
一种方法是映射顶层元素,然后筛选,然后将groupMembers中的每个列表映射为仅包含mD值的内容。
%dw 2.0
output application/json
---
payload map { 
        country: $.country,
        LC:  $.groupMembers 
                filter ($.mD) 
                map ($.dN) 
    } 
输出:
[
  {
    "country": "AT",
    "LC": [
      "268"
    ]
  },
  {
    "country": "RO",
    "LC": [
      "273"
    ]
  }
]
英文:
One way is to map the top level elements then filter then map each list in groupMembers to only the value of mD.
%dw 2.0
output application/json
---
payload map { 
        country: $.country,
        LC:  $.groupMembers 
                filter ($.mD) 
                map ($.dN) 
    } 
Output:
[
  {
    "country": "AT",
    "LC": [
      "268"
    ]
  },
  {
    "country": "RO",
    "LC": [
      "273"
    ]
  }
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论