I want to push a value after matching id value.
I have comments JSON which has property : reply
[{
"id": "1",
"root_id": "1",
"target_id": "12",
"text": "Hello World!",
"comment_posted_by": "John",
"comment_posted_by_image": "/../../assets/images/no-user.png",
"comment_time": "2 mins ago",
"reply": [{
"id": "123",
"root_id": "1",
"target_id": "222",
"text": "Nice!"
},
{
"id": "124",
"root_id": "1",
"target_id": "222",
"text": "Good!"
}
]
},
{
"id": "2",
"root_id": "2",
"target_id": "12",
"text": "Hello RG!",
"comment_posted_by": "John",
"comment_posted_by_image": "/../../assets/images/no-user.png",
"comment_time": "2 mins ago",
"reply": [{
"id": "123",
"root_id": "1",
"target_id": "222",
"text": "Hey john how are you?"
},
{
"id": "124",
"root_id": "1",
"target_id": "222",
"text": "Hi john!"
}
]
}, {
"id": "3",
"root_id": "3",
"target_id": "12",
"text": "Thanks.",
"comment_posted_by": "John",
"comment_posted_by_image": "/../../assets/images/no-user.png",
"comment_time": "2 mins ago"
}, {
"id": "4",
"root_id": "4",
"target_id": "12",
"text": "Great.",
"comment_posted_by": "John",
"comment_posted_by_image": "/../../assets/images/no-user.png",
"comment_time": "2 mins ago"
},
{
"id": "5",
"root_id": "5",
"target_id": "12",
"text": "Welcome.",
"comment_posted_by": "John",
"comment_posted_by_image": "/../../assets/images/no-user.png",
"comment_time": "2 mins ago"
}
]
Comments and their Reply I want to push a reply on by using below HTML code:
HTML
<div [attr.commentId]="commentData.id" class="addReplyContainer replyTextAreaContainer" style="display: none" >
<textarea (keyup.enter)="addReply($event,commenData.root_id)" [(ngModel)]="reply" style="width:100%" class="replyText commentText addReplyTextarea form-control"></textarea>
</div>
and want to push newly made reply json into array mentioned above:
var reply =
{
"id": "125",
"root_id":1,
"target_id":"222",
"text":"Great!"
}
Is there a way to do this using $.each?
I am trying below method but it is giving me error:
addReply(event, root_id)
{
let commentDataa = this.commentsData;
let replyy = this.reply;
jQuery.each(this.commentsData, function (index, value) {
if(value.root_id == root_id)
{
commentDataa.reply.push({
root_id:root_id,
target_id: "12",
text: replyy,
});
}
})
}
Error: ERROR TypeError: Cannot read property 'push' of undefined
Any guidance would be appreciated. Thanks in ADVANCE.
indexincommentDataa