How can I get a map/reduce result which is sorted within descending order of the quot;valuequot; value?if also using list function can achieve that?(如何获得按“值降序排序的 map/reduce 结果?值?如果也使用列表函数可以实现吗?)
问题描述
我有这样的视图地图和减少:地图:
I have view map and reduce like this: Map:
function(doc) {
if(doc.type){
var usersLength = doc.users.length;
for (var i = 0; i < usersLength ; i++) {
emit([doc.users[i].userid,doc.Service.ownId], 1);
}
}
}
减少:
function(keys, values, rereduce)
{
return sum(values);
}
然后像这样调用视图:_design/aaa/_view/getUserId?group=true&group_level=2&startkey=["111111"]&endkey=["111111",{}]代码>
我可以得到下面的结果集:
I can get the result set below:
{"rows":[
{"key":["111111",""],"value":7},
{"key":["111111","FFFF26"],"value":1},
{"key":["111111","FFFF44"],"value":7},
{"key":["111111","FFFF34"],"value":2}
]}
但是,我想让上面的结果集按值"值的降序排序.如果还使用列表功能可以实现吗?预期结果如下:
However, I want to get the result set above sorted within descending order of the "value" value. if also using list function can achieve that? the following would be expected results:
{"rows":[
{"key":["111111",""],"value":7},
{"key":["111111","FFFF44"],"value":7},
{"key":["111111","FFFF34"],"value":2},
{"key":["111111","FFFF26"],"value":1}
]}
推荐答案
如果要对结果集进行排序,则必须按值降序排序,可以使用比较器回调进行排序:
If you want to sort the result set you have to one that is sorted by value descending you can sort with a comparator callback:
// ES6 Syntax
let originalResult = {
"rows":[
{"key":["111111",""],"value":7},
{"key":["111111","FFFF26"],"value":1},
{"key":["111111","FFFF44"],"value":7},
{"key":["111111","FFFF34"],"value":2}
]}
let newResult = {
rows: originalResult.rows.sort((a, b) => b.value - a.value)
}
这篇关于如何获得按“值"降序排序的 map/reduce 结果?值?如果也使用列表函数可以实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获得按“值"降序排序的 map/reduce 结果?值?如果也使用列表函数可以实现吗?
基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01