javascript - 如何在将值插入javascript中的数组时匹配键

标签 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",
        "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>

并希望将新制作的回复 json 推送到上面提到的数组中:

var reply =
{
"id": "125",
"root_id":1,
"target_id":"222",
"text":"Great!"
}

有没有办法使用 $.each 来做到这一点?

我正在尝试下面的方法,但它给我错误:

 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

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

最佳答案

您需要访问 commentDataa 数组的 index 以便您可以在 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
      });
    }
  });
}

关于javascript - 如何在将值插入javascript中的数组时匹配键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49726928/

相关文章:

javascript - 如何在 Node js 后端服务器中创建一个对象并将其传递给 Angular 2 前端

javascript - fullcalendar 如何将 titleFormat 设置为 m/d/yyyy - m/d/yyyy

javascript - Jest : select an HTML element through innerHTML

javascript - 在直接 JS 中使用 AJAX 进行连续数据库调用

c# - 将 C# 变量传递给 JS 的最佳方式

javascript - 什么在函数中创建函数?

javascript - 如何在 Angular 5 中调用外部 javascript 函数?

javascript - 嵌套的可观察对象仅在超时后传播错误

html - intro.js 中是否有任何选项可以使突出显示的文本或图像清晰

angular - 如何禁用 Angular 2 中的复选框