在JavaScript中对注释进行分组

时间:2014-12-11 19:29:08

标签: javascript underscore.js

我有一系列评论对象。每个注释都有一个primary_key,comment_text,date_posted和thread_id。 thread_id引用父注释的primary_key。

鉴于以下三条评论

var comments = [
    {content: "parent comment", primary_key: 1, date_posted: 12/10/14, thread_id: null},
    {content: "another parent comment", primary_key: 2, date_posted: 12/11/14, thread_id: null},
    {content: "reply", primary_key: 3, date_posted: 12/11/14, thread_id: 1}
]

如何返回一系列已排序的注释,其中所有对父注释的回复都将遵循父注释(最好按日期排序)?

我已经能够使用

打破回复
_.filter(comments, function(comment) { return comment.thread })  

但我正在努力将它们与评论相协调

期望的输出将是

[
    {content: "parent comment", primary_key: 1, date_posted: 12/10/14, thread_id: null},
    {content: "reply", primary_key: 3, date_posted: 12/11/14, thread_id: 1},
    {content: "another parent comment", primary_key: 2, date_posted: 12/11/14, thread_id: null}
]

小提琴:http://jsfiddle.net/2ewaxgdu/

0 个答案:

没有答案