DataWeave-如何在数组中的一个对象中以相同的顺序加入SKU列表

发布于 2025-01-24 15:28:03 字数 789 浏览 0 评论 0原文

如何在数组中的一个对象中以相同的顺序加入SKU列表。

输入:

[{
 "number": "7358",
 "sku": "301-01",
 "desc": "1"
}, {
 "number": "7358",
 "sku": "301-02",
 "desc": "2"
}, {
 "number": "7359",
 "sku": "301-03",
 "desc": "3"
}, {
 "number": "7359",
 "sku": "301-03",
 "desc": "4"
}, {
 "number": "7360",
 "sku": "301-03",
 "desc": "5"
}]

输出:

[{
  "number": "7358",
  "list": [{
      "sku": "301-01",
      "desc": "1"
  }, {
      "sku": "301-02",
      "desc": "2"
  }]
}, {
  "number": "7359",
  "list": [{
      "sku": "301-03",
      "desc": "3"
  }, {
      "sku": "301-03",
      "desc": "4"
  }]
}, {
  "number": "7360",
  "list": [{
      "sku": "301-03",
      "desc": "5"
  }]
}]

输出我们只创建一个新的“列表”数组,其中包含具有共同顺序的SKU和DESC。任何帮助将不胜感激。谢谢。

How to join the list of sku with same order in one object in the array.

Input:

[{
 "number": "7358",
 "sku": "301-01",
 "desc": "1"
}, {
 "number": "7358",
 "sku": "301-02",
 "desc": "2"
}, {
 "number": "7359",
 "sku": "301-03",
 "desc": "3"
}, {
 "number": "7359",
 "sku": "301-03",
 "desc": "4"
}, {
 "number": "7360",
 "sku": "301-03",
 "desc": "5"
}]

Output:

[{
  "number": "7358",
  "list": [{
      "sku": "301-01",
      "desc": "1"
  }, {
      "sku": "301-02",
      "desc": "2"
  }]
}, {
  "number": "7359",
  "list": [{
      "sku": "301-03",
      "desc": "3"
  }, {
      "sku": "301-03",
      "desc": "4"
  }]
}, {
  "number": "7360",
  "list": [{
      "sku": "301-03",
      "desc": "5"
  }]
}]

Output we just create a new "list" array that contains the skus and the desc that have the order in common. Any help would be appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉城メ夜风 2025-01-31 15:28:04

嗨,萨利姆(Salim)所示的最好方法是我进行了一些修改,

%dw 2.0
output application/json
---
payload 
    groupBy ((item, index) -> item.number)
    mapObject ((value, key, index) -> 
        {
            number: key,
            list: value map ((item, index) -> item - "number") //We remove the number field
        }
    )

简单的组件是组的关键是组的标准,因此它是数字,最简单的重新构建输出结构的方法是通过执行地图和删除。 “数字”字段

Hi The best way as shown by Salim is the groupBy I did a few modifications

%dw 2.0
output application/json
---
payload 
    groupBy ((item, index) -> item.number)
    mapObject ((value, key, index) -> 
        {
            number: key,
            list: value map ((item, index) -> item - "number") //We remove the number field
        }
    )

Simple the key of the groupBy is the criteria that was group so it is the number and the simplest way to re build the output structure is by doing the map and removing the "number" field

愿与i 2025-01-31 15:28:04

尝试此脚本:

%dw 2.0
output application/json
---
payload groupBy $.number mapObject ((value, key, index) -> {
           number: value.number[0],
           list: value reduce ((item1, a =[]) -> a + { sku: item1.sku ,  desc: item1.desc})
}
)

这是与输出匹配的更新脚本:

%dw 2.0
output application/json
---
payload groupBy $.number pluck $ map ((item,index) -> {
           number: item.number[0],
           list: item reduce ((item1, a =[]) -> a + { sku: item1.sku ,  desc: item1.desc})
}
)

Try this script:

%dw 2.0
output application/json
---
payload groupBy $.number mapObject ((value, key, index) -> {
           number: value.number[0],
           list: value reduce ((item1, a =[]) -> a + { sku: item1.sku ,  desc: item1.desc})
}
)

Here is the updated script that matches the output:

%dw 2.0
output application/json
---
payload groupBy $.number pluck $ map ((item,index) -> {
           number: item.number[0],
           list: item reduce ((item1, a =[]) -> a + { sku: item1.sku ,  desc: item1.desc})
}
)
雪落纷纷 2025-01-31 15:28:04

正如其他经验丰富的成员所提到的那样,groupby是实现这一目标的正确方法。但是,我注意到给定方法的输出有差异。以下脚本将输出一系列项目。

脚本将根据评论中提供的新更改而更新以添加运输日期。假设是订单(编号)的运输日期。

%dw 2.0
output application/json
var itemByNumber = payload groupBy ((item) -> item.number)
var removeKeys = ["number", "shippingDate"]
---
keysOf(itemByNumber) map ((key) -> 
do  {
    var item = itemByNumber[key]
    ---
    {
        number: key,
        shippingDate: item[0].shippingDate,
        list: item map ($ -- removeKeys)
    }
})

输出

[
  {
    "number": "7358",
    "shippingDate": "28/04/2022",
    "list": [
      {
        "sku": "301-01",
        "desc": "1"
      },
      {
        "sku": "301-02",
        "desc": "2"
      }
    ]
  },
  {
    "number": "7359",
    "shippingDate": "29/04/2022",
    "list": [
      {
        "sku": "301-03",
        "desc": "3"
      },
      {
        "sku": "301-03",
        "desc": "4"
      }
    ]
  },
  {
    "number": "7360",
    "shippingDate": "27/04/2022",
    "list": [
      {
        "sku": "301-03",
        "desc": "5"
      }
    ]
  }
]

输入

[
  {
    "number": "7358",
    "shippingDate":"28/04/2022",
    "sku": "301-01",
    "desc": "1"
  },
  {
    "number": "7358",
    "shippingDate":"28/04/2022",
    "sku": "301-02",
    "desc": "2"
  },
  {
    "number": "7359",
    "shippingDate":"29/04/2022",
    "sku": "301-03",
    "desc": "3"
  },
  {
    "number": "7359",
    "shippingDate":"29/04/2022",
    "sku": "301-03",
    "desc": "4"
  },
  {
    "number": "7360",
    "shippingDate":"27/04/2022",
    "sku": "301-03",
    "desc": "5"
  }
]

As other experienced members have mentioned, groupBy is the right way to achieve this. However, I noticed there is a difference in the output with the given approaches. The following script will output an array of items.

Script is updated to add shippingDate based on the new changes provided in the comment. Assumption is shippingDate is same for an order (number).

%dw 2.0
output application/json
var itemByNumber = payload groupBy ((item) -> item.number)
var removeKeys = ["number", "shippingDate"]
---
keysOf(itemByNumber) map ((key) -> 
do  {
    var item = itemByNumber[key]
    ---
    {
        number: key,
        shippingDate: item[0].shippingDate,
        list: item map ($ -- removeKeys)
    }
})

Output

[
  {
    "number": "7358",
    "shippingDate": "28/04/2022",
    "list": [
      {
        "sku": "301-01",
        "desc": "1"
      },
      {
        "sku": "301-02",
        "desc": "2"
      }
    ]
  },
  {
    "number": "7359",
    "shippingDate": "29/04/2022",
    "list": [
      {
        "sku": "301-03",
        "desc": "3"
      },
      {
        "sku": "301-03",
        "desc": "4"
      }
    ]
  },
  {
    "number": "7360",
    "shippingDate": "27/04/2022",
    "list": [
      {
        "sku": "301-03",
        "desc": "5"
      }
    ]
  }
]

Input

[
  {
    "number": "7358",
    "shippingDate":"28/04/2022",
    "sku": "301-01",
    "desc": "1"
  },
  {
    "number": "7358",
    "shippingDate":"28/04/2022",
    "sku": "301-02",
    "desc": "2"
  },
  {
    "number": "7359",
    "shippingDate":"29/04/2022",
    "sku": "301-03",
    "desc": "3"
  },
  {
    "number": "7359",
    "shippingDate":"29/04/2022",
    "sku": "301-03",
    "desc": "4"
  },
  {
    "number": "7360",
    "shippingDate":"27/04/2022",
    "sku": "301-03",
    "desc": "5"
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文