如何根据条件映射一个数组

huangapple go评论50阅读模式
英文:

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"
    ]
  }
]

huangapple
  • 本文由 发表于 2023年2月23日 22:36:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定