我有这样的视图地图和减少:地图:
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 结果?值?如果也使用列表函数可以实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)