我如何根据 mulesoft 数据编织中的列值放置顺序计数或索引?

how do i place a sequential count or index based on a column value in mulesoft data weave?

我需要根据另一列中的值在一列中写一个 sequence/index。 请在下面找到示例来解释我的请求: 在这里,我需要根据颜色列中的值在自定义索引列中生成索引值。所以基本上,如果列中有 4 行红色,那么它应该将这些行索引为 1、2、3、4,然后当它以蓝色为值的 2 行,它应该再次从 1 -> 1,2 索引。

输入数据:

[
  {
    "Type": "Header",
    "Color": "Red",
    "Customindex": ""
  },
  {
    "Type": "Header",
    "Color": "Blue",
    "Customindex": ""
  },
  {
    "Type": "LineItem",
    "Color": "Red",
    "Customindex": ""
  },
  {
    "Type": "LineItem",
    "Color": "Red",
    "Customindex": ""
  },
  {
    "Type": "LineItem",
    "Color": "Blue",
    "Customindex": ""
  },
  {
    "Type": "Header",
    "Color": "Yellow",
    "Customindex": ""
  },
  {
    "Type": "LineItem",
    "Color": "Yellow",
    "Customindex": ""
  }
]

这是我需要帮助的转换:

%dw 2.0
output application/json
var TM = flatten(payload map ((item, index) ->
[{
"Type":payload.type, //HEADER VALUES
"Color":"",
"Customindex":""
}]++
[{
"Type":payload.type,// LINE ITEM VALUES
"Color":payload.color,
"Customindex":index // need the index sequence to only count similar colors.. For ex : red start with 1,2,3 then for blue again 1,2 and so on
}]))
---
(((TM distinctBy $ ... filters and groupings

带有详细信息的样本数据(这是预期的输出):

试试这个,内联添加了评论:

%dw 2.0
output application/csv

var data = [
  {
    "Type": "Header",
    "Customindex": "",
    "Color": "Red"
  },
  {
    "Type": "Header",
    "Color": "Blue",
    "Customindex": ""
  },
  {
    "Type": "LineItem",
    "Color": "Red",
    "Customindex": ""
  },
  {
    "Type": "LineItem",
    "Color": "Red",
    "Customindex": ""
  },
  {
    "Type": "LineItem",
    "Color": "Blue",
    "Customindex": ""
  },
  {
    "Type": "Header",
    "Color": "Yellow",
    "Customindex": ""
  },
  {
    "Type": "LineItem",
    "Color": "Yellow",
    "Customindex": ""
  }
]
filter ($.Type != "Header")

// Create a map that contains the value of color as the key and the current-index
// in the value.
var vs2idx = data.*Color distinctBy $ 
             reduce (e,acc={}) -> acc ++ {(e): 1}
             
// Set the header object to be used later on
var header = {Type: "Header",Color: "", Customindex: ""}

---
/*
 * The accumulator contain two fields:
 *  1. The env that contains the state that keeps the color counters
 *  2. The result that contains the rows with the Customindex value
 * This data structure keeps re-created for every single row--this is necessary
 * because data in dataweave are immutable--i.e. we can't make any in-place
 * updates.
 */
(data reduce (row,acc={env: vs2idx, result: []}) -> do {
    var r = acc.result + (row update {
        case s at .Customindex -> acc.env[row.Color]
    })
    var e = {(acc.env - row.Color), (row.Color): acc.env[row.Color]+1}
    ---
    {env: e, result: r}
}).result
// group by color
groupBy $.Color
// Get the values of the resulting object
pluck $
// Iterate over the values and inject the header
reduce (e,acc=[]) -> acc + header ++ e

虽然我输出的数据是JSON,但很容易转换成CSV格式。

这是我使用过的DW功能的文档:

  1. reduce
  2. Local variables
  3. Multi-value selector
  4. update operator

编辑:更新表达式以生成 CSV,而不更改索引的位置并注入 header 行。