简体   繁体   中英

Get count of object property based on condition

I have a array of objects like below:

[  
   {  
      "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"
   }
]

All the objects contain a topicId and comments (can be empty/null) property. I want to know the count of all the comments based on topicId which is like a primary key.

So I get to know how many users have commented on each topic. I tried some thing like this:

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

But I don't think its the correct way.

How do I do this?

You could use an object for counting.

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

Then you get the count in the object, like

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

You can create a new object that will have the topic ID for the key and the number of comments for the value

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

almost same as previous answers, but has less readability, but also might be a bit fast on large collections.

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

a rough implementation of: http://underscorejs.org/docs/underscore.html#section-45

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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