根据条件获取对象属性的计数

时间:2017-02-01 12:36:37

标签: javascript

我有一系列对象如下:

[  
   {  
      "commentId":1485594783811,
      "topicId":"1485594764668",
      "comments":"hi2",
      "commentDate":"1/31/2017, 12:59:08 PM",
      "userImage":"assets/img/spiritual-icon4.png",
      "username":"k****@gmail.com"
   },
   {  
      "commentId":1485866129370,
      "topicId":"1485853106269",
      "comments":"Hi",
      "commentDate":"1/31/2017, 6:05:29 PM",
      "userImage":"assets/img/spiritual-icon4.png",
      "username":"kv****@gmail.com"
   },
   {  
      "commentId":1485939547285,
      "topicId":"1485853106269",
      "comments":"Hi",
      "commentDate":"2/1/2017, 3:18:34 PM",
      "userImage":"assets/img/spiritual-icon4.png",
      "username":"ki*****9@gmail.com"
   },
   {  
      "commentId":1485947026195,
      "topicId":"1485945483238",
      "comments":"hi",
      "commentDate":"2/1/2017, 4:33:46 PM",
      "userImage":"assets/img/spiritual-icon4.png",
      "username":"ki****9@gmail.com"
   }
]

所有对象都包含topicIdcomments(可以为空/ null)属性。我想知道基于topicId的所有评论的计数,它就像一个主键。

因此,我了解了有多少用户对每个主题发表了评论。我试过这样的事情:

var count = 0;
res.forEach(function(el, i){
    self.data.topicIdArr.push(el.topicId);
});

self.data.topicIdArr.forEach(function(el, i){
    if(res[i].topicId == el){
        self.data.topicIdArr.push(count++);
    }
});

但我不认为这是正确的方法。

我该怎么做?

3 个答案:

答案 0 :(得分:3)

您可以使用对象进行计数。



var data = [{ commentId: 1485594783811, topicId: 1485594764668, comments: "hi2", commentDate: "1/31/2017, 12:59:08 PM", userImage: "assets/img/spiritual-icon4.png", username: "k****@gmail.com" }, { commentId: 1485866129370, topicId: 1485853106269, comments: "Hi", commentDate: "1/31/2017, 6:05:29 PM", userImage: "assets/img/spiritual-icon4.png", username: "kv****@gmail.com" }, { commentId: 1485939547285, topicId: 1485853106269, comments: "Hi", commentDate: "2/1/2017, 3:18:34 PM", userImage: "assets/img/spiritual-icon4.png", username: "ki*****9@gmail.com" }, { commentId: 1485947026195, topicId: 1485945483238, comments: "hi", commentDate: "2/1/2017, 4:33:46 PM", userImage: "assets/img/spiritual-icon4.png", username: "ki****9@gmail.com" }],
    count = Object.create(null);

data.forEach(function (a) {
    count[a.topicId] = (count[a.topicId] || 0) + 1;
});

console.log(count);

.as-console-wrapper { max-height: 100% !important; top: 0; }




然后你得到对象的计数,比如

{
   "1485594764668": 1,
   "1485853106269": 2,
   "1485945483238": 1
}

答案 1 :(得分:3)

您可以创建一个新对象,该对象将具有该键的主题ID和该值的注释数



var myArray = [{
  "commentDate": "1/31/2017, 12:59:08 PM",
  "commentId": 1485594783811,
  "comments": "hi2",
  "topicId": "1485594764668",
  "userImage": "assets/img/spiritual-icon4.png",
  "username": "ki******99@gmail.com"
}, {
  "commentDate": "1/30/2017, 12:59:08 PM",
  "commentId": 1485594783812,
  "comments": "hello",
  "topicId": "1485594764669",
  "userImage": "assets/img/spiritual-icon4.png",
  "username": "ki******99@gmail.com"
}, {
  "commentDate": "1/29/2017, 12:59:08 PM",
  "commentId": 1485594783813,
  "comments": "Hi man !",
  "topicId": "1485594764668",
  "userImage": "assets/img/spiritual-icon4.png",
  "username": "ki******99@gmail.com"
}];

var result = {};

myArray.forEach(function(o){
  result[o.topicId] = result[o.topicId] || 0;
  result[o.topicId]++;
});

console.log(result);




答案 2 :(得分:1)

与之前的答案几乎相同,但可读性较差,但在大型集合上也可能有点快。

wrong.output[] <- lapply(wrong.output, as.numeric)

粗略执行:http://underscorejs.org/docs/underscore.html#section-45