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?