简体   繁体   English

用循环将内部对象附加到多维数组中

[英]Append inner objects in multi-dimensional array with a loop

I have a multi-dimensional array of comment objects and I figured out how I can loop through the first-level of objects and then append them, but I am having a really hard time figuring out how to bring the replies along with the comments that have replies. 我有一个多维的注释对象数组,我想出了如何遍历对象的第一级然后附加它们的方法,但是我很难弄清楚如何将回复和注释一起带入。有回覆。 My strategy was to use an "if" statement that checks to see if there's a reply, and then loops through the replies like I do the comments, but I think that the closest() function is the issue, I'm not quite sure how to access the DOM element that corresponds to that comment to use it as a selector. 我的策略是使用“ if”语句检查是否有答复,然后像我做注释一样循环遍历所有答复,但是我认为最接近的函数是问题,我不太确定如何访问与该注释对应的DOM元素以将其用作选择器。 I think maybe another strategy would be to process the replies at the same time as the comments with one .each loop. 我认为也许另一种策略是使用一个.each循环与评论同时处理回复。 Any help would be greatly appreciated! 任何帮助将不胜感激!

        var myCommentArray = [
        {
            _id: "888888888888888888",
            index: 1,
            name: "Perez",
            message: "First Comment .......",
            subject: "enim officias",
            replies: [ // notice this comment has replies (just 1 but it is still an array)
              {
                  _id: "77777777777777777777",
                  index: 0,
                  name: "Reply to First Comment Ines for Perez",
                  message: "...",
                  subject: "reply subject consequat"
              }
            ]
        },
              {
                  _id: "999999999999",
                  index: 0,
                  name: "Shelton",
                  message: "2nd Comment....a",
                  subject: "enim irure",
                  replies: null // notice this comment has no replies and as such is null. this is better than an empty array
              },
              {
                  _id: "666666666666666666",
                  index: 2,
                  name: "Perez",
                  message: "3rd Comment.......",
                  subject: "enim officias",
                  replies: [
                    {
                        _id: "55555555555555555555",
                        index: 0,
                        name: "1st Reply to 3rd Comment",
                        message: "...",
                        subject: "reply subject consequat"
                    },
                     {
                         _id: "44444444444444444444",
                         index: 1,
                         name: "2nd Reply to 3rd Comment",
                         message: "...",
                         subject: "reply subject consequat"
                     }
                  ]
              }
            ];


        sabio.page.processComments = function (i, currentComment) {
            var commentsFormat = '<br> <div class="comment-avatar media-left"> <img src="http://placehold.it/50x50" alt="avatar">' +
           '</div><div class="comment-content media-body clearfix"> <div class="comment-avatar media-left"></div><h3 class="media-heading">' +
           currentComment.subject + '</h3> <div class="comment-meta">By ' + currentComment.name + '</div> <div class="comment-body"> <p>'
           + currentComment.message + '</p><a href="#" class="replyButton">' +
           '<i class="fa fa-reply"> </i> Reply </a> </div> </div>';

            $('.comments').append(commentsFormat);

         };

        $.each(myCommentArray, sabio.page.processComments);

    sabio.page.processReplies = function (j, currentComment) {
    var repliesFormat = '<br> <div class="comment-avatar media-left"> <img src="http://placehold.it/50x50" alt="avatar">' +
      '</div><div class="comment-content media-body clearfix"> <div class="comment-avatar media-left"></div><h3 class="media-heading">' + currentComment.subject + '</h3> <div class="comment-meta">By ' +      currentComment.name + '</div> <div class="comment-body"> <p>' +           currentComment.message + '</p><a href="#" class="btn btn-gray more  reply">' +
      '<i class="fa fa-reply"> </i> Reply </a> </div> </div>';



   var closestComment= $(currentComment).closest();


  $(closestComment).append(repliesFormat);
    });

    if (myCommentArray.replies) {

      $.each(myCommentArray.replies, sabio.page.processReplies);
  };

There is definitely some misunderstanding here of what jQuery's .closest takes as a parameter and there are some curious scoping errors with currentComment . 对于jQuery的.closest作为参数,肯定存在一些误解,并且currentComment存在一些奇怪的作用域错误。 However, the primary thing to fix is that if replies is a property of each comment object, then you will need to process the replies for each comment item - that is, the code to process replies should be handled somehow by the processComments function. 但是,要解决的主要问题是,如果replies是每个注释对象的属性,那么您将需要处理每个评论项目的回复-也就是说,处理回复的代码应由processComments函数以某种方式处理。

With that said, I will propose to you what I think is a better solution to your problem. 话虽如此,我将向您提出我认为是您的问题的更好解决方案的建议。 Because you are trying to render HTML from your comments array, I think a much cleaner and elegant solution would be to use a JavaScript template. 因为您正在尝试从注释数组中呈现HTML,所以我认为一种更简洁的解决方案是使用JavaScript模板。 I will use the .template method from the Underscore.js library: 我将使用来自该.template方法Underscore.js库:

<script id="CommentsTemplate" type="text/template">
    <div>
        <% _.each(comments, function (comment) { %>
            <div class="comment-avatar media-left">
                <img src="http://placehold.it/50x50" alt="avatar" />
            </div>
            <div class="comment-content media-body clearfix">
                <div class="comment-avatar media-left"></div>
                <h3 class="media-heading"><%- comment.subject %></h3>
                <div class="comment-meta">By <%- comment.name %></div>
                <div class="comment-body">
                    <p><%- comment.message %></p>
                    <a href="#" class="replyButton"><i class="fa fa-reply"> </i> Reply</a>
                </div>
            </div>
            <div>
                <% _.each(comment.replies, function (reply) { %>
                    <div class="comment-avatar media-left">
                        <img src="http://placehold.it/50x50" alt="avatar" />
                    </div>
                    <div class="comment-content media-body clearfix">
                        <div class="comment-avatar media-left"></div>
                        <h3 class="media-heading"><%- reply.subject %></h3>
                        <div class="comment-meta">By <%- reply.name %></div>
                        <div class="comment-body">
                            <p><%- reply.message %></p>
                            <a href="#" class="btn btn-gray more reply"><i class="fa fa-reply"> </i> Reply</a>
                        </div>
                    </div>
                <% }); %>
            </div>
        <% }); %>
    </div>
</script>

With our template in place, our rendering can be reduced to some very simple code: 有了模板之后,我们的渲染可以简化为一些非常简单的代码:

var template = _.template($('#CommentsTemplate').html());
$('.comments').html(template({ comments: comments }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM