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

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

How can I map an array with a condition

问题

所以我需要映射一个dN如果mD为true。但我不确定如何添加条件来检查,到目前为止我最远的进展是映射所有的dN。

这是有效载荷:

  1. [{
  2. "country" : "AT",
  3. "groupMembers": [
  4. {
  5. "dN": "268",
  6. "mD": true
  7. },
  8. {
  9. "dN": "240",
  10. "mD": false
  11. }
  12. ]
  13. },
  14. {
  15. "country" : "RO",
  16. "groupMembers": [
  17. {
  18. "dN": "273",
  19. "mD": true
  20. },
  21. {
  22. "dN": "292",
  23. "mD": false
  24. }
  25. ]
  26. }]

我所做的是这样的:

  1. payload map (value,index) -> { country : value.country,
  2. LC: value.groupMembers.dN }

并得到了以下输出:

  1. [{
  2. "country" : "at",
  3. "LC": [
  4. "268",
  5. "240"
  6. ]
  7. },
  8. {
  9. "country" : "pt",
  10. "LC": [
  11. "273",
  12. "292"
  13. ]
  14. }]

我如何才能让只有"268"和"273"被映射,因为"MD"中的true语句?
预期的结果应该类似于:

  1. [{
  2. "country" : "at",
  3. "LC": "268"
  4. },
  5. {
  6. "country" : "pt",
  7. "LC": "273"
  8. }]
英文:

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:

  1. [{
  2. "country" : "AT",
  3. "groupMembers": [
  4. {
  5. "dN": "268",
  6. "mD": true
  7. },
  8. {
  9. "dN": "240",
  10. "mD": false
  11. }
  12. ]
  13. },
  14. {
  15. "country" : "RO",
  16. "groupMembers": [
  17. {
  18. "dN": "273",
  19. "mD": true
  20. },
  21. {
  22. "dN": "292",
  23. "mD": false
  24. }
  25. ]
  26. }]

What I did was this:

  1. payload map (value,index) -> { country : value.country,
  2. LC: value.groupMembers.dN }

And got this Output:

  1. [
  2. {
  3. "country" : "at",
  4. "LC": [
  5. "268",
  6. "240"
  7. ]
  8. },
  9. {
  10. "country" : "pt",
  11. "LC": [
  12. "273",
  13. "292"
  14. ]
  15. }]

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 :

  1. [
  2. { "country" : "at",
  3. "LC": "268"
  4. },
  5. { "country" : "pt",
  6. "LC": "273"
  7. }
  8. ]

答案1

得分: 0

以下脚本将帮助您。

  1. %dw 2.0
  2. output application/json
  3. ---
  4. LC : (payload.groupMembers flatMap ($ filter($.mD == true))) map (
  5. ($.dN)
  6. )
英文:

Below script will help you.

  1. %dw 2.0
  2. output application/json
  3. ---
  4. LC : (payload.groupMembers flatMap ($ filter($.mD == true))) map (
  5. ($.dN)
  6. )

答案2

得分: 0

一种方法是映射顶层元素,然后筛选,然后将groupMembers中的每个列表映射为仅包含mD值的内容。

  1. %dw 2.0
  2. output application/json
  3. ---
  4. payload map {
  5. country: $.country,
  6. LC: $.groupMembers
  7. filter ($.mD)
  8. map ($.dN)
  9. }

输出:

  1. [
  2. {
  3. "country": "AT",
  4. "LC": [
  5. "268"
  6. ]
  7. },
  8. {
  9. "country": "RO",
  10. "LC": [
  11. "273"
  12. ]
  13. }
  14. ]
英文:

One way is to map the top level elements then filter then map each list in groupMembers to only the value of mD.

  1. %dw 2.0
  2. output application/json
  3. ---
  4. payload map {
  5. country: $.country,
  6. LC: $.groupMembers
  7. filter ($.mD)
  8. map ($.dN)
  9. }

Output:

  1. [
  2. {
  3. "country": "AT",
  4. "LC": [
  5. "268"
  6. ]
  7. },
  8. {
  9. "country": "RO",
  10. "LC": [
  11. "273"
  12. ]
  13. }
  14. ]

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:

确定