合并2 ko observableArrays以创建一个嵌套数组

时间:2014-10-14 13:24:32

标签: arrays knockout.js

我有2个Observable Arrays。 的注释

  "Comments": [
{
  "CommentID": 5,
  "CommentDateCreated": "1 hour ago",
  "CommentUserName": "Jacques",
  "CommentUserEmail": "http://www.gravatar.com/avatar/b40e3df0130dc8cd9bf864f438bfad4e?d=mm&r=g",
  "CommentText": "Well this seems to be working. Next I have to do the\n<blockquote cite=\"jim\">\nReplies!\n</blockquote >",
  "CommentReplies": []
},
{
  "CommentID": 6,
  "CommentDateCreated": "1 hour ago",
  "CommentUserName": "Jacques",
  "CommentUserEmail": "http://www.gravatar.com/avatar/b40e3df0130dc8cd9bf864f438bfad4e?d=mm&r=g",
  "CommentText": "This is another Reply to test comments counter",
  "CommentReplies": []
},
{
  "CommentID": 7,
  "CommentDateCreated": "1 hour ago",
  "CommentUserName": "Jacques",
  "CommentUserEmail": "http://www.gravatar.com/avatar/b40e3df0130dc8cd9bf864f438bfad4e?d=mm&r=g",
  "CommentText": "Well this is to test the <strike>placeholder</strike>",
  "CommentReplies": []
},
{
  "CommentID": 8,
  "CommentDateCreated": "8 minutes ago",
  "CommentUserName": "Jacques",
  "CommentUserEmail": "http://www.gravatar.com/avatar/b40e3df0130dc8cd9bf864f438bfad4e?d=mm&r=g",
  "CommentText": "Hi another Comment",
  "CommentReplies": []
}
],

回复数组

"Replies": [
{
  "CommentID": 8,
  "CommentRepliesText": "Test 4",
  "CommentRepliesUserEmail": "http://www.gravatar.com/avatar/5e543256c480ac577d30f76f9120eb74?d=mm&r=g"
},
{
  "CommentID": 8,
  "CommentRepliesText": "Test 9",
  "CommentRepliesUserEmail": "http://www.gravatar.com/avatar/5e543256c480ac577d30f76f9120eb74?d=mm&r=g"
},
{
  "CommentID": 6,
  "CommentRepliesText": "This is another test",
  "CommentRepliesUserEmail": "http://www.gravatar.com/avatar/5e543256c480ac577d30f76f9120eb74?d=mm&r=g"
},
{
  "CommentID": 5,
  "CommentRepliesText": "This is a test reply",
  "CommentRepliesUserEmail": "http://www.gravatar.com/avatar/5e543256c480ac577d30f76f9120eb74?d=mm&r=g"
}
],

我想从这两个数组中创建一个CommentsAndReplies observable Array。回复应该与每个评论嵌套,以便我可以将它们绑定到我的元素。那么

  

Comments.CommentID === Replies.CommentID

我想将对特定注释的回复推送到CommentsAndReplies数组中。

所以我想这样的事情:

CommentsAndReplies [{
Comments": [
    {
      "CommentID": 5,
      "CommentDateCreated": "1 hour ago",
      "CommentUserName": "Jacques",
      "CommentUserEmail": "http://www.gravatar.com/avatar/b40e3df0130dc8cd9bf864f438bfad4e?d=mm&r=g",
      "CommentText": "Well this seems to be working. Next I have to do the\n<blockquote cite=\"jim\">\nReplies!\n</blockquote >"
      "CommentReplies": [ {
                     "CommentID": 5,
                     "CommentRepliesText": "This is a test reply",
                     "CommentRepliesUserEmail": "http://www.gravatar.com/avatar/5e543256c480ac577d30f76f9120eb74?d=mm&r=g"
             }
        ]
    },

}]

1 个答案:

答案 0 :(得分:1)

重新映射注释数组并添加到每个注释所有回复结果过滤使用CommentId回复数组:

var CommentsAndReplies = Comments.map(function(comment) {
    comment.CommentReplies = Replies.filter(function(reply) {
        return reply.CommentID === comment.CommentID;
    });
    return comment;
});

演示:JSFiddle