为没有循环的数组中的每个对象赋值

时间:2018-05-16 14:50:46

标签: javascript arrays angularjs object angularjs-ng-repeat

我在angular 1应用程序中添加了一个简单的注释组件。我从服务器获得的对象如下所示:

comments":[
        {"id":1,"content":"this is my first comment","approved":1,"replies":[]},
        {"id":2,"content":"this is the second","approved":1,"replies":[
          {"priority_id":0,"content":"content of a reply","approved":1},
          {"priority_id":0,"content":"content of a second reply","approved":1}]
        }
      ]

目前我在控制器中使用2个循环来分配"待定"每个评论的价值和已批准的回复:0

看起来像这样:

angular.forEach($scope.comments, function (comment) {
          if (comment.approved === 0) {
            comment.pending = true;
        }
          comment.replies = comment.replies || [];

          angular.forEach(comment.replies, function (reply) {
          if (reply.approved === 0) {
            reply.pending = true;
            }
          });
      });

是否有更好,更短的方法来获得它? Javascript并不完全是我的强项。我真的需要在控制器中循环吗?也许我可以以某种方式传递ng-repeat的值?提前谢谢!

感谢Joe我知道一种比循环更好的方法。我已经将他的答案标记为解决方案,虽然我必须稍微修改代码才能工作,因为我的应用程序使用了与箭头函数不兼容的角度1.4。这是代码:

$scope.comments.map(function(comment) {

                  if (comment.approved === 0) {
                    comment.pending = true;
                  }

                  comment.replies = comment.replies || [];

                  comment.replies.map(function(reply){
                     if (reply.approved === 0) {
                    reply.pending = true;
                    }
                    return reply.approved;
                  });
                  return comment.approved;

                });

它看起来并不像Joe那样整洁,但它仍然使用map方法,正如Joe和Matthew所建议的那样,它比循环更好。

2 个答案:

答案 0 :(得分:4)

所以不确定这是否更明确或更简洁,但你可以利用数组上的javascript .map原型。 map函数的作用是迭代数组,并根据提供的函数将每个值转换为新值。所以这实际上会返回从前一个数组映射的新对象数组。我不确定这是否真的更简单。您还可以利用ES5箭头功能。

另一件事: 在当前代码中,如果批准=== 0,则注释仅获取待处理字段。这意味着如果不等于0,则注释中将不存在挂起字段。如果你依赖这个领域,这可能会在以后产生奇怪的事情。有些人会说等待为真,而其他评论则是未定义的。您的回复也是如此。我的解决方案使用条件AS作为挂起字段,以便它存在于每个对象上,而不仅仅是真实对象。

MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

$scope.comments.map(comment => {
    comment.pending = comment.approved === 0;
    comment.replies = (comment.replies || []).map(reply => {
        reply.pending = reply.approved === 0;
        return reply;
    });
    return comment;
})

答案 1 :(得分:0)

您可以编写一个将该键添加到对象的函数。使用此功能,您的循环将如下所示:

public void doGet(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException  {

     RequestDispatcher rd = request.getRequestDispatcher("jsp/doactiontwo.jsp");
     rd.forward(request, response);
}

此外,您可以升级此函数以接收对象并检查条件,如果条件为真,则添加密钥。在let comments = [{ "id": 1, "content": "this is my first comment", "approved": 1, "replies": [] }, { "id": 2, "content": "this is the second", "approved": 1, "replies": [{ "priority_id": 0, "content": "content of a reply", "approved": 1 }, { "priority_id": 0, "content": "content of a second reply", "approved": 1 } ] }, { "id": 3, "content": "this is the third", "approved": 0, "replies": [{ "priority_id": 0, "content": "content of a reply", "approved": 0 }, { "priority_id": 0, "content": "content of a second reply", "approved": 0 } ] }]; function AddPending(obj) { obj['pending'] = true; return obj; } comments.forEach(function(comment) { comment = (comment.hasOwnProperty('approved') && comment.approved === 0) ? AddPending(comment) : comment; comment.replies = comment.replies || []; comment.replies.forEach(function(reply) { reply = (reply.approved === 0) ? AddPending(reply) : reply; }); }); console.log(comments);上使用此功能。如下所示

ng-repeat
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.comments = [{
    "id": 1,
    "content": "this is my first comment",
    "approved": 1,
    "replies": []
  }, {
    "id": 2,
    "content": "this is the second",
    "approved": 1,
    "replies": [{
        "priority_id": 0,
        "content": "content of a reply",
        "approved": 1
      },
      {
        "priority_id": 0,
        "content": "content of a second reply",
        "approved": 1
      }
    ]
  }, {
    "id": 3,
    "content": "this is the third",
    "approved": 0,
    "replies": [{
        "priority_id": 0,
        "content": "content of a reply",
        "approved": 0
      },
      {
        "priority_id": 0,
        "content": "content of a second reply",
        "approved": 0
      }
    ]
  }];

  function AddPending(obj) {
    obj['pending'] = true;
    return obj;
  }

  $scope.f = function(comment) {
    comment = (comment.approved === 0) ? AddPending(comment) : comment;
    comment.replies = comment.replies || [];

    angular.forEach(comment.replies, function(reply) {
      reply = (reply.approved === 0) ? AddPending(reply) : reply;
    });
    return comment;
  };

});