how to append comments in feed html use jquery

Viewed 111
[
    {
        "id": 0,
        "owner": {
            "id": 0,
            "email": "",
            "username": "",
            "profile": "",
            "description": "",
            "followers": 0,
            "following": 0
        },
        "title": "",
        "content": "",
        "image_urls": [
            "image_url1",
            "image_url2",
            "image_url3", ...
        ],
        "liked_people": [
            1,
            2,
            3, ...
        ],
        "like_count": 0,
        "comment_count": 0,
        "view_count": 0,
        "tag" : [
          "tag1",
          "tag2",
          "tag3", ...
        ],
        "created_at": "",
        "comment_preview": [
            {
                "comment_id": 0,
                "owner": {
                    "id": 0,
                    "email": "",
                    "username": "",
                    "profile": "",
                    "description": "",
                    "followers": 0,
                    "following": 0
                },
                "content": "",
                "created_at": ""
            }, ...
        ]
    }, .....
]

This is api what feed and detail data.

I am developing SNS.

I get this data and I want to create feed list what having 2 comments but comments is may or may not be exist.

So how to append this comments to html?

Here is example code Please add comments html is .feedComment.

const getFeed = (id) => {
$.ajax({
    url: `BASE_URL/feed/postList`,
    type: 'GET',
    success: function(res) {            
        res.map(({id, owner, image_urls, title, content, tag, comment_preview}) => {  
            $('.feedArea').append(`
               <div class='feed'>
                 <div class='feedHead'>
                     <div class='postmanProfile'>
                             <img src= "${owner.image}">                     
                     </div>
                     <div class='postmanName'>
                         <div class= 'postmanNameText'>${owner.username}</div>
                     </div>                     
                 </div>
                 <div class='disuniteLine'>
                     <hr>
                 </div>
                 <div class="feedThumbanil">
                     <div class="thumbanilImg">
                         <a href="detailFeed.html?id=${id}">
                             <img class="ImgArea" src="${image_urls}" alt="">
                         </a>
                     </div>
                 </div>
                 <div class="disuniteLine">
                     <hr>
                 </div>
                 <div class='feedLike_And_Comment'>
                     <div class='LikeIconImg like-${id}' feedId="${id}">

                     </div>
                     <div class="CommentIconImg">
                         <img class="commentImg" src="/picture/Icon/chat-box.png" alt="#">
                     </div>
                     <div class="viewMoreArea">
                         <img class="view_more" src="/picture/Icon/more.png">
                     </div>
                 </div>
                 <div class='title'>
                     <h2>
                         ${title}
                     </h2>
                 </div>
                 <div class='content'>
                     <span>
                         ${content}
                     </span>
                 </div>
                 <div class='tag'>
                     <h4>
                         ${tag}
                     </h4>
                 </div>
                 <div class="feedComment">
                    // What should I do?   
                 </div>
               </div>
            `);
        })   
    },
    error : function(err){
        console.log(err);
    },
    async: false    
})

}

I am time-limited life. If I don't solve this, I might die.

1 Answers

Assuming your ajax works, you basically need this

<div class="feedComment">
   ${Object.keys(comment_preview[0].owner)
     .map(key => (key+":"+comment_preview[0].owner[key]))
    }
</div> 

const res = [{ "id": 0, "owner": { "id": 0, "email": "", "username": "", "profile": "", "description": "", "followers": 0, "following": 0 }, "title": "", "content": "", "image_urls": [ "image_url1", "image_url2", "image_url3" ], "liked_people": [ 1, 2, 3 ], "like_count": 0, "comment_count": 0, "view_count": 0, "tag": [ "tag1", "tag2", "tag3" ], "created_at": "", "comment_preview": [{ "comment_id": 0, "owner": { "id": 0, "email": "bla@bla.com", "username": "User x ", "profile": "x profile", "description": "Desc", "followers": 0, "following": 0 }, "content": "Is this a feed comment?", "created_at": "" }] }] 


function format(res) {
  res.forEach(({
    id,
    owner,
    image_urls,
    title,
    content,
    tag,
    comment_preview
  }) => {
    $('.feedArea').append(`
             <div class='feed'>
                 <div class='feedHead'>
                     <div class='postmanProfile'>
                             <img src= "${owner.image}">                     
                     </div>
                     <div class='postmanName'>
                         <div class= 'postmanNameText'>${owner.username}</div>
                     </div                     
                </div>
                 <div class='disuniteLine'>
                     <hr>
                 </div>
                 <div class="feedThumbanil">
                     <div class="thumbanilImg">
                         <a href="detailFeed.html?id=${id}">
                             <img class="ImgArea" src="${image_urls}" alt="">
                         </a>
                     </div>
                 </div>
                 <div class="disuniteLine">
                     <hr>
                 </div>
                 <div class='feedLike_And_Comment'>
                     <div class='LikeIconImg like-${id}' feedId="${id}">

                     </div>
                     <div class="CommentIconImg">
                         <img class="commentImg" src="/picture/Icon/chat-box.png" alt="#">
                     </div>
                     <div class="viewMoreArea">
                         <img class="view_more" src="/picture/Icon/more.png">
                     </div>
                 </div>
                 <div class='title'>
                     <h2>
                         ${title}
                     </h2>
                 </div>
                 <div class='content'>
                     <span>
                         ${content}
                     </span>
                 </div>
                 <div class='tag'>
                     <h4>
                         ${tag}
                     </h4>
                 </div>
                 <div class="feedComment">
                   ${Object.keys(comment_preview[0].owner)   
                     .map(key => (key+":"+comment_preview[0].owner[key]))
                   }
                </div> 
            `);
  })
}
format(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="feedArea"></div>

Related