Dataweave:根据分数创建排名

Séb*_*las 2 dataweave

使用以下输入:

[5,5,4,4,4,2,2,1]
Run Code Online (Sandbox Code Playgroud)

我想生成以下输出:

[
  {
    "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
  }
]
Run Code Online (Sandbox Code Playgroud)

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

The*_*Guy 5

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

%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
}
Run Code Online (Sandbox Code Playgroud)

请让我知道你的想法。