繁体   English   中英

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

[英]Get count of object property based on condition

我有如下的对象数组:

[  
   {  
      "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 (可以为空/空)属性。 我想知道所有基于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++);
    }
});

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

我该怎么做呢?

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

 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
}

您可以创建一个新对象,该对象将具有键的主题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); 

几乎与以前的答案相同,但可读性较低,但在大型馆藏中可能会有点快。

> a
[ { 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' } ]
> counts = {}
> for(var i=0; i<a.length; i++) {
... counts[a[i].topicId] = counts.hasOwnProperty(a[i].topicId) ? counts[a[i].topicId]+1 : 1;
... }
1
> counts
{ '1485594764668': 1, '1485853106269': 2, '1485945483238': 1 }
> 

一个粗略的实现: http : //underscorejs.org/docs/underscore.html#section-45

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM