簡體   English   中英

benchdb簡化為矩陣

[英]couchdb reduce to a matrix

我有一個ouchdb,上面有一系列的文檔

{
  "foo" :1,
  "bar" : 2
}
{
  "foo" :3,
  "bar" : 4
}

並試圖創建一個減少到

{
   "ColumnNames" : ["foo","bar"],
   "Values" : [[1,3],[2,4]] 
}

但目前仍停留在:

function (keys, values, rereduce) {
    var firstPass = {};
    for(var i = 0; i < values.length; i++ ){
        var v = values[i];
        for(var prop in v){
            if(v.hasOwnProperty(prop)){
                if (!firstPass.hasOwnProperty(prop)) {
                    firstPass[prop] = [];
                }
                var ve = v[prop];
                firstPass[prop].push(ve);
            }
        }
    }
    return firstPass;
}

減少到

{
   "foo" : [[1],[3]],
   "bar" : [[2],[4]]
}

我不知道如何獲得其余的方式

Map將數據分組的良好用例,然后將分組分組后傳遞給結果對象

 const data = [{ "foo": 1, "bar": 2 }, { "foo": 3, "bar": 4 } ]; const m = new Map() data.forEach(e => { Object.entries(e).forEach(([k, v]) => { !m.has(k) && m.set(k, []); m.get(k).push(v); }); }); const res = { ColumnNames: [...m.keys()], Values: [...m.values()] } console.log(res) 
 .as-console-wrapper { max-height: 100% !important;} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM