1

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.

3
  • 1
    commentDataa is an array so commentDataa[index].reply.push() should work. Commented Apr 9, 2018 at 6:47
  • 1
    You are missing index in commentDataa Commented Apr 9, 2018 at 6:48
  • Thank you Everyone. Let me try this. Commented Apr 9, 2018 at 6:49

1 Answer 1

1

You need to access the index of the commentDataa array so that you can push reply in that particular object of commentDataa[index] object.

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[index].reply.push({
        root_id: root_id,
        target_id: '12',
        text: replyy
      });
    }
  });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.