簡體   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