D
D
Devero972021-12-20 20:32:32
JavaScript
Devero97, 2021-12-20 20:32:32

How to calculate the average rating like in mongoDB, only in pure js?

I have a post:

post: {
      title: "Future devops", 
      avgrating: 5, 
      ratingOne: 4
      ratingTwo: 4 
      ratingThree: 4       
      ratingFour: 5 
      ratingFive: 4
    }

and comments on it
commentsPost: [
      {
      name: "Dima", 
      ratingOne: 3,
      ratingTwo: 2,
      ratingThree: 2,      
      ratingFour: 5,
      ratingFive: 2
      },
      {
      name: "Dima", 
      ratingOne: 3,
      ratingTwo: 2,
      ratingThree: 2,      
      ratingFour: 5,
      ratingFive: 2
      },
      {
      name: "Dima", 
      ratingOne: 3,
      ratingTwo: 2,
      ratingThree: 2,      
      ratingFour: 5,
      ratingFive: 2
      },
      {
      name: "Dima", 
      ratingOne: 3,
      ratingTwo: 2,
      ratingThree: 2,      
      ratingFour: 5,
      ratingFive: 2
      },
      {
      name: "Dima", 
      ratingOne: 3,
      ratingTwo: 2,
      ratingThree: 2,      
      ratingFour: 5,
      ratingFive: 2
      },
     ]

The maximum score of the parameter is 5 points.

I want to calculate the average rating for each Post's rating field based on the total number of comments and their ratings.

That is, each Post field (ratingOne, ratingTwo, ratingThree,
ratingFour, ratingFive) has an average value for the number of comments. And the post's avgrating field has an average score based on the rest of the Post's rating fields (ratingOne, etc.).

The bottom line is that people put ratings on 5 parameters (speed, accessibility, etc.) and based on this, the overall rating of the service and the overall rating for each parameter are calculated. In order to evaluate the service in detail for each parameter.

I'm scratching my head, but I can't think of a better way to implement it. Mongoose has a parameter for this and everything is done there in a couple of lines, but I need to implement this in pure js, since I use graphcms, and there is no such possibility as aggregate - avg, only count.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question