Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中将值推入数组时如何匹配键_Javascript_Angular - Fatal编程技术网

在javascript中将值推入数组时如何匹配键

在javascript中将值推入数组时如何匹配键,javascript,angular,Javascript,Angular,我想在匹配id值后推送一个值 我有注释JSON,其属性为: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",

我想在匹配id值后推送一个值

我有注释JSON,其属性为:
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"
    }
]
评论及其回复 我想使用以下HTML代码推送回复:

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>
是否有一种方法可以使用$进行此操作

我正在尝试以下方法,但它给了我错误:

 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,

          });

        }
    })

  }
错误:错误类型错误:无法读取未定义的属性“push”

任何指导都将不胜感激。
提前感谢。

您需要访问
commentDataa
数组的
索引
,以便可以在
commentDataa[index]
对象的特定对象中推送回复

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
      });
    }
  });
}

commentDataa是一个数组,因此commentDataa[index].reply.push()应该可以工作。您在
commentDataa
中缺少
index
,谢谢大家。让我试试这个。
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
      });
    }
  });
}