在javascript中计算数组中的注释数

时间:2017-03-30 08:24:34

标签: javascript arrays

我有这样的数组结构:

[Array[2].attributes, Array[6].attributes] contains list of objects

第一个数组包含post_id的博文,第二个数组包含commentcomment_id和{的博客评论{1}}

我想计算每个帖子的评论数量,并为post_id

的每个对象在第一个数组(即博客文章数组)中插入计数

完整结构:

post_id

我一直在尝试这样但却无法正常工作:

[  
   [  
      {  
         "attributes":{  
            "created_at":"3/30/2017, 1:33:37 AM",
            "details":"aaa",
            "id":116,
            "post_id":"1490817817738",
            "title":" Technologies - Learn to code Java, Spring, Angular etc",
            "url":"-technologies---learn-to-code-java,-spring,-angular-etc",
            "userImage":"assets/img/spiritual-icon4.png",
            "username":".k999@gmail.com"
         }
      },
      {  
         "attributes":{  
            "created_at":"3/30/2017, 12:11:28 PM",
            "details":"aaa",
            "id":117,
            "post_id":"1490856088450",
            "title":"Express JS Tutorial",
            "url":"express-js-tutorial",
            "userImage":"assets/img/spiritual-icon4.png",
            "username":".k999@gmail.com"
         }
      }
   ],
   [  
      {  
         "attributes":{  
            "comment_id":"1490817936990",
            "content":"hi",
            "created_at":"2017-03-29 20:05:36",
            "id":28,
            "likes":"",
            "post_id":"1490817817738",
            "userImage":"https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
            "username":".k999@gmail.com"
         }
      },
      {  
         "attributes":{  
            "comment_id":"1490856043965",
            "content":"hi",
            "created_at":"2017-03-30 06:40:43",
            "id":29,
            "likes":"",
            "post_id":"1490817817738",
            "userImage":"https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
            "username":".k999@gmail.com"
         }
      },
      {  
         "attributes":{  
            "comment_id":"1490856048963",
            "content":"hwllo",
            "created_at":"2017-03-30 06:40:48",
            "id":30,
            "likes":"",
            "post_id":"1490817817738",
            "userImage":"https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
            "username":".k999@gmail.com"
         }
      },
      {  
         "attributes":{  
            "comment_id":"1490856056938",
            "content":"hello",
            "created_at":"2017-03-30 06:40:56",
            "id":31,
            "likes":"",
            "post_id":"1490817817738",
            "userImage":"https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
            "username":".k999@gmail.com"
         }
      },
      {  
         "attributes":{  
            "comment_id":"1490856093828",
            "content":"aaa",
            "created_at":"2017-03-30 06:41:33",
            "id":32,
            "likes":"",
            "post_id":"1490856088450",
            "userImage":"https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
            "username":".k999@gmail.com"
         },
      },
      {  
         "attributes":{  
            "comment_id":"1490856096261",
            "content":"sss",
            "created_at":"2017-03-30 06:41:36",
            "id":33,
            "likes":"",
            "post_id":"1490856088450",
            "userImage":"https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
            "username":".k999@gmail.com"
         },
      }
   ]
]

更新

var commentCount = Object.create(null);
self.postId = Object.create(null);

res[1].forEach(function(el , i) {
    console.log('el ', el);
   commentCount[el.attributes.post_id] = (commentCount[el.attributes.comment_id] || 0) +1;
});
console.log('self.commentCount ', commentCount);

1 个答案:

答案 0 :(得分:1)

基本上,您不使用post_id的相同参考。

commentCount[el.attributes.post_id] = (commentCount[el.attributes.comment_id] || 0) + 1
//                         ^^^^^^^                                ^^^^^^^^^^

但你需要

commentCount[el.attributes.post_id] = (commentCount[el.attributes.post_id] || 0) + 1
//                         ^^^^^^^                                ^^^^^^^

您可以迭代第二个数组并计算给定post_id

的出现次数



var data = [[{ attributes: { created_at: "3/30/2017, 1:33:37 AM", details: "aaa", id: 116, post_id: 1490817817738, title: " Technologies - Learn to code Java, Spring, Angular etc", url: "-technologies---learn-to-code-java,-spring,-angular-etc", userImage: "assets/img/spiritual-icon4.png", username: ".k999@gmail.com" }, dirtyAttributeNames: [], frozen: false, metaModelLocal: { columnMetadata: { created_at: { columnName: "created_at", typeName: "TEXT", columnSize: 65535 }, details: { columnName: "details", typeName: "TEXT", columnSize: 65535 }, id: { columnName: "id", typeName: "MEDIUMINT", columnSize: 7 }, post_id: { columnName: "post_id", typeName: "TEXT", columnSize: 65535 }, title: { columnName: "title", typeName: "TEXT", columnSize: 65535 }, url: { columnName: "url", typeName: "VARCHAR", columnSize: 1000 }, userImage: { columnName: "userImage", typeName: "VARCHAR", columnSize: 45 }, username: { columnName: "username", typeName: "VARCHAR", columnSize: 45 } }, associations: [{ fkName: "post_id", source: "com..seeker.models.Post", target: "com..seeker.models.PostsCategories" }, { fkName: "post_id", source: "com..seeker.models.Post", target: "com..seeker.models.Comment" }], idName: "id", tableName: "post", dbType: "MySQL", dbName: "default", modelClass: "com..seeker.models.Post", cached: false, versionColumn: "record_version" }, modelRegistryLocal: { callbacks: [], attributeConverters: {}, validators: [] }, cachedParents: {}, cachedChildren: {}, manageTime: true, compositeKeyPersisted: false, errors: {} }, { attributes: { created_at: "3/30/2017, 12:11:28 PM", details: "aaa", id: 117, post_id: 1490856088450, title: "Express JS Tutorial", url: "express-js-tutorial", userImage: "assets/img/spiritual-icon4.png", username: ".k999@gmail.com" }, dirtyAttributeNames: [], frozen: false, metaModelLocal: { columnMetadata: { created_at: { columnName: "created_at", typeName: "TEXT", columnSize: 65535 }, details: { columnName: "details", typeName: "TEXT", columnSize: 65535 }, id: { columnName: "id", typeName: "MEDIUMINT", columnSize: 7 }, post_id: { columnName: "post_id", typeName: "TEXT", columnSize: 65535 }, title: { columnName: "title", typeName: "TEXT", columnSize: 65535 }, url: { columnName: "url", typeName: "VARCHAR", columnSize: 1000 }, userImage: { columnName: "userImage", typeName: "VARCHAR", columnSize: 45 }, username: { columnName: "username", typeName: "VARCHAR", columnSize: 45 } }, associations: [{ fkName: "post_id", source: "com..seeker.models.Post", target: "com..seeker.models.PostsCategories" }, { fkName: "post_id", source: "com..seeker.models.Post", target: "com..seeker.models.Comment" }], idName: "id", tableName: "post", dbType: "MySQL", dbName: "default", modelClass: "com..seeker.models.Post", cached: false, versionColumn: "record_version" }, modelRegistryLocal: { callbacks: [], attributeConverters: {}, validators: [] }, cachedParents: {}, cachedChildren: {}, manageTime: true, compositeKeyPersisted: false, errors: {} }], [{ attributes: { comment_id: 1490817936990, content: "hi", created_at: "2017-03-29 20:05:36", id: 28, likes: 0, post_id: 1490817817738, userImage: "https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50", username: ".k999@gmail.com" }, dirtyAttributeNames: [], frozen: false, metaModelLocal: { columnMetadata: { comment_id: { columnName: "comment_id", typeName: "TEXT", columnSize: 65535 }, content: { columnName: "content", typeName: "TEXT", columnSize: 65535 }, created_at: { columnName: "created_at", typeName: "TEXT", columnSize: 65535 }, id: { columnName: "id", typeName: "MEDIUMINT", columnSize: 7 }, likes: { columnName: "likes", typeName: "TEXT", columnSize: 65535 }, post_id: { columnName: "post_id", typeName: "TEXT", columnSize: 65535 }, updated_at: { columnName: "updated_at", typeName: "TEXT", columnSize: 65535 }, userImage: { columnName: "userImage", typeName: "VARCHAR", columnSize: 200 }, username: { columnName: "username", typeName: "VARCHAR", columnSize: 200 } }, associations: [{ fkName: "comment_id", source: "com..seeker.models.Comment", target: "com..seeker.models.CommentRating" }, { fkName: "post_id", source: "com..seeker.models.Comment", target: "com..seeker.models.Post" }], idName: "id", tableName: "comment", dbType: "MySQL", dbName: "default", modelClass: "com..seeker.models.Comment", cached: false, versionColumn: "record_version" }, modelRegistryLocal: { callbacks: [], attributeConverters: {}, validators: [] }, cachedParents: {}, cachedChildren: {}, manageTime: true, compositeKeyPersisted: false, errors: {} }, { attributes: { comment_id: 1490856043965, content: "hi", created_at: "2017-03-30 06:40:43", id: 29, likes: 0, post_id: 1490817817738, userImage: "https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50", username: ".k999@gmail.com" }, dirtyAttributeNames: [], frozen: false, metaModelLocal: { columnMetadata: { comment_id: { columnName: "comment_id", typeName: "TEXT", columnSize: 65535 }, content: { columnName: "content", typeName: "TEXT", columnSize: 65535 }, created_at: { columnName: "created_at", typeName: "TEXT", columnSize: 65535 }, id: { columnName: "id", typeName: "MEDIUMINT", columnSize: 7 }, likes: { columnName: "likes", typeName: "TEXT", columnSize: 65535 }, post_id: { columnName: "post_id", typeName: "TEXT", columnSize: 65535 }, updated_at: { columnName: "updated_at", typeName: "TEXT", columnSize: 65535 }, userImage: { columnName: "userImage", typeName: "VARCHAR", columnSize: 200 }, username: { columnName: "username", typeName: "VARCHAR", columnSize: 200 } }, associations: [{ fkName: "comment_id", source: "com..seeker.models.Comment", target: "com..seeker.models.CommentRating" }, { fkName: "post_id", source: "com..seeker.models.Comment", target: "com..seeker.models.Post" }], idName: "id", tableName: "comment", dbType: "MySQL", dbName: "default", modelClass: "com..seeker.models.Comment", cached: false, versionColumn: "record_version" }, modelRegistryLocal: { callbacks: [], attributeConverters: {}, validators: [] }, cachedParents: {}, cachedChildren: {}, manageTime: true, compositeKeyPersisted: false, errors: {} }, { attributes: { comment_id: 1490856048963, content: "hwllo", created_at: "2017-03-30 06:40:48", id: 30, likes: 0, post_id: 1490817817738, userImage: "https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50", username: ".k999@gmail.com" }, dirtyAttributeNames: [], frozen: false, metaModelLocal: { columnMetadata: { comment_id: { columnName: "comment_id", typeName: "TEXT", columnSize: 65535 }, content: { columnName: "content", typeName: "TEXT", columnSize: 65535 }, created_at: { columnName: "created_at", typeName: "TEXT", columnSize: 65535 }, id: { columnName: "id", typeName: "MEDIUMINT", columnSize: 7 }, likes: { columnName: "likes", typeName: "TEXT", columnSize: 65535 }, post_id: { columnName: "post_id", typeName: "TEXT", columnSize: 65535 }, updated_at: { columnName: "updated_at", typeName: "TEXT", columnSize: 65535 }, userImage: { columnName: "userImage", typeName: "VARCHAR", columnSize: 200 }, username: { columnName: "username", typeName: "VARCHAR", columnSize: 200 } }, associations: [{ fkName: "comment_id", source: "com..seeker.models.Comment", target: "com..seeker.models.CommentRating" }, { fkName: "post_id", source: "com..seeker.models.Comment", target: "com..seeker.models.Post" }], idName: "id", tableName: "comment", dbType: "MySQL", dbName: "default", modelClass: "com..seeker.models.Comment", cached: false, versionColumn: "record_version" }, modelRegistryLocal: { callbacks: [], attributeConverters: {}, validators: [] }, cachedParents: {}, cachedChildren: {}, manageTime: true, compositeKeyPersisted: false, errors: {} }, { attributes: { comment_id: 1490856056938, content: "hello", created_at: "2017-03-30 06:40:56", id: 31, likes: 0, post_id: 1490817817738, userImage: "https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50", username: ".k999@gmail.com" }, dirtyAttributeNames: [], frozen: false, metaModelLocal: { columnMetadata: { comment_id: { columnName: "comment_id", typeName: "TEXT", columnSize: 65535 }, content: { columnName: "content", typeName: "TEXT", columnSize: 65535 }, created_at: { columnName: "created_at", typeName: "TEXT", columnSize: 65535 }, id: { columnName: "id", typeName: "MEDIUMINT", columnSize: 7 }, likes: { columnName: "likes", typeName: "TEXT", columnSize: 65535 }, post_id: { columnName: "post_id", typeName: "TEXT", columnSize: 65535 }, updated_at: { columnName: "updated_at", typeName: "TEXT", columnSize: 65535 }, userImage: { columnName: "userImage", typeName: "VARCHAR", columnSize: 200 }, username: { columnName: "username", typeName: "VARCHAR", columnSize: 200 } }, associations: [{ fkName: "comment_id", source: "com..seeker.models.Comment", target: "com..seeker.models.CommentRating" }, { fkName: "post_id", source: "com..seeker.models.Comment", target: "com..seeker.models.Post" }], idName: "id", tableName: "comment", dbType: "MySQL", dbName: "default", modelClass: "com..seeker.models.Comment", cached: false, versionColumn: "record_version" }, modelRegistryLocal: { callbacks: [], attributeConverters: {}, validators: [] }, cachedParents: {}, cachedChildren: {}, manageTime: true, compositeKeyPersisted: false, errors: {} }, { attributes: { comment_id: 1490856093828, content: "aaa", created_at: "2017-03-30 06:41:33", id: 32, likes: 0, post_id: 1490856088450, userImage: "https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50", username: ".k999@gmail.com" }, dirtyAttributeNames: [], frozen: false, metaModelLocal: { columnMetadata: { comment_id: { columnName: "comment_id", typeName: "TEXT", columnSize: 65535 }, content: { columnName: "content", typeName: "TEXT", columnSize: 65535 }, created_at: { columnName: "created_at", typeName: "TEXT", columnSize: 65535 }, id: { columnName: "id", typeName: "MEDIUMINT", columnSize: 7 }, likes: { columnName: "likes", typeName: "TEXT", columnSize: 65535 }, post_id: { columnName: "post_id", typeName: "TEXT", columnSize: 65535 }, updated_at: { columnName: "updated_at", typeName: "TEXT", columnSize: 65535 }, userImage: { columnName: "userImage", typeName: "VARCHAR", columnSize: 200 }, username: { columnName: "username", typeName: "VARCHAR", columnSize: 200 } }, associations: [{ fkName: "comment_id", source: "com..seeker.models.Comment", target: "com..seeker.models.CommentRating" }, { fkName: "post_id", source: "com..seeker.models.Comment", target: "com..seeker.models.Post" }], idName: "id", tableName: "comment", dbType: "MySQL", dbName: "default", modelClass: "com..seeker.models.Comment", cached: false, versionColumn: "record_version" }, modelRegistryLocal: { callbacks: [], attributeConverters: {}, validators: [] }, cachedParents: {}, cachedChildren: {}, manageTime: true, compositeKeyPersisted: false, errors: {} }, { attributes: { comment_id: 1490856096261, content: "sss", created_at: "2017-03-30 06:41:36", id: 33, likes: 0, post_id: 1490856088450, userImage: "https://lh3.googleusercontent.com/-/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50", username: ".k999@gmail.com" }, dirtyAttributeNames: [], frozen: false, metaModelLocal: { columnMetadata: { comment_id: { columnName: "comment_id", typeName: "TEXT", columnSize: 65535 }, content: { columnName: "content", typeName: "TEXT", columnSize: 65535 }, created_at: { columnName: "created_at", typeName: "TEXT", columnSize: 65535 }, id: { columnName: "id", typeName: "MEDIUMINT", columnSize: 7 }, likes: { columnName: "likes", typeName: "TEXT", columnSize: 65535 }, post_id: { columnName: "post_id", typeName: "TEXT", columnSize: 65535 }, updated_at: { columnName: "updated_at", typeName: "TEXT", columnSize: 65535 }, userImage: { columnName: "userImage", typeName: "VARCHAR", columnSize: 200 }, username: { columnName: "username", typeName: "VARCHAR", columnSize: 200 } }, associations: [{ fkName: "comment_id", source: "com..seeker.models.Comment", target: "com..seeker.models.CommentRating" }, { fkName: "post_id", source: "com..seeker.models.Comment", target: "com..seeker.models.Post" }], idName: "id", tableName: "comment", dbType: "MySQL", dbName: "default", modelClass: "com..seeker.models.Comment", cached: false, versionColumn: "record_version" }, modelRegistryLocal: { callbacks: [], attributeConverters: {}, validators: [] }, cachedParents: {}, cachedChildren: {}, manageTime: true, compositeKeyPersisted: false, errors: {} }]],
        count = Object.create(null);

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

console.log(count);

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