2017-07-31 62 views
0

我一直在尝试在多个级别使用组,以便对象按标记,文件夹和报告键进行分组。我能够以单一级别进行分组,但无法继续进行。多级别组由ramda js

const data = [{ 
 
    "_index": "search_index", 
 
    "_type": "search_type", 
 
    "_id": "Co5EFxnqeo9ruq", 
 
    "_score": 1, 
 
    "_source": { 
 
    "admin_tags": [], 
 
    "type": "human", 
 
    "folder": "Feature Samples", 
 
    "url_t": "url_tdata", 
 
    "users": [ 
 
     128 
 
    ], 
 
    "agent_id": 2, 
 
    "url": "url_data", 
 
    "favorited_by": [20], 
 
    "idx": 121, 
 
    "path": "Report Samples//Feature Samples", 
 
    "name": "Grouping and Sorting", 
 
    "description": "" 
 
    } 
 
}]; 
 

 
const mapIndexed = R.addIndex(R.map); 
 

 
const tg = mapIndexed((x, i) => ({ 
 
    "name": x._source.admin_tags[0] == undefined ? "Unclasified" : x._source.admin_tags[0], 
 
    "idx": i, 
 
    "folder": [{ 
 
    "name": x._source.folder, 
 
    "idx": x._source.folder, 
 
    "report": [{ 
 
     "agent_id": x._source.agent_id, 
 
     "description": x._source.description, 
 
     "favorited_by": x._source.favorited_by[0], 
 
     "name": x._source.name, 
 
     "idx": x._source.idx, 
 
     "path": x._source.path, 
 
     "type": x._source.type, 
 
     "url": x._source.url, 
 
     "url_t": x._source.url_t, 
 
    }] 
 
    }] 
 
}), data); 
 

 
const byTag = R.groupBy(data => data.name) 
 
const tagged = byTag(tg); 
 
console.log(tagged)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js"></script>

这是我想在三个层面和user_report_id的值与分组来实现输出,意见总是被设置为零。

const output = { 
    "message": "", 
    "payload": [{ 
    "name": "Unclasified", 
    "idx": 0, 
    "folders": [{ 
     "idx": "Feature Samples", 
     "name": "Feature Samples", 
     "reports": [{ 
     "agent_id": 2, 
     "description": "", 
     "idx": 121, 
     "is_fav": 20, 
     "name": "Grouping and Sorting", 
     "type": "human", 
     "url": "url_data", 
     "url_t": "url_tdata", 
     "user_report_id": 0, 
     "views": 0, 
     "report_id": '2sdfsd' 
     }] 
    }] 
    }] 
} 
+0

在这里,我已经初始化子数组文件夹和报告,但他们没有分组。因为可以有多个文件夹和报告 – user93

回答

0

有可能是一个更好的/内置的解决方案,但是你可以用map,以便将对象的“内部名单”,例如:

const data = [ 
    { letter: "a", number: 1, bool: true }, 
    { letter: "a", number: 2, bool: true }, 
    { letter: "a", number: 2, bool: false }, 
    { letter: "b", number: 1, bool: true }, 
    { letter: "b", number: 1, bool: false }, 
    { letter: "b", number: 1, bool: false }, 
    { letter: "c", number: 1, bool: true }, 
    { letter: "c", number: 2, bool: true }, 
    { letter: "c", number: 2, bool: false }, 
    { letter: "c", number: 2, bool: true }, 
]; 

const groupByLetterNumberBool = pipe(
    groupBy(prop("letter")), 
    map(groupBy(prop("number"))), 
    map(map(groupBy(prop("bool")))) 
); 

groupByLetterNumberBool(data); 

工作实例here