dataweave - 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
}
]
我找到了一个解决方案(见回复),但我想知道是否有更好的方法来做到这一点。
解决方案
我不确定这是否涵盖了您所有的测试用例,但这是我想出的较短版本的代码 -
%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
}
请让我知道你的想法。
推荐阅读
- c++ - cpp中z3的编译和执行命令?
- docker - FeathersJS 和 Docker 应用程序存在套接字和连接问题
- xml - 如何将 xml 文件“复制”到 PostgreSQL?
- sql-server - 恢复时备份 SQL Server Developer 大喊它启用了数据压缩,仅在企业版上可用
- c - 我的 if 语句与我希望它做的相反
- mysql - AND , OR 子句在 MYSQL 中对我不起作用
- python - 查看字符串是否包含不同数据帧中的子字符串
- amazon-cognito - AWS Cognito - preAuthentication 和 customMessage 触发器
- vue.js - Vue 不使用 Webpack 渲染模板,只考虑运行时构建
- python - 无法在 Visual Studio Code 中调试 Flask 应用程序