首页 > 解决方案 > Dataweave:根据分数创建排名

问题描述

使用以下输入:

[5,5,4,4,4,2,2,1]

我想生成以下输出:

[
  {
    "points": 5,
    "rank": 1
  },
  {
    "points": 5,
    "rank": 1
  },
  {
    "points": 4,
    "rank": 3
  },
  {
    "points": 4,
    "rank": 3
  },
  {
    "points": 4,
    "rank": 3
  },
  {
    "points": 2,
    "rank": 6
  },
  {
    "points": 2,
    "rank": 6
  },
  {
    "points": 1,
    "rank": 8
  }
]

我找到了一个解决方案(见回复),但我想知道是否有更好的方法来做到这一点。

标签: dataweave

解决方案


我不确定这是否涵盖了您所有的测试用例,但这是我想出的较短版本的代码 -

%dw 2.0
import * from dw::core::Arrays
output application/json
var inp=[5,5,4,4,4,2,2,1]
---
inp map (v0,k0) ->
{
    points:v0,
    rank:indexOf(inp,v0)+1
}

请让我知道你的想法。


推荐阅读