本文介紹了如何獲得按“值"降序排序的 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 結果?值?如果也使用列表函數可以實現嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!