0

I have table list of organization and table review of each organization then I want to calculate the average rate of each organization (one of the attributes in review table) This is JSON file that I have

{
   id: 17,
   name_org: "AAA company",
   picture: "default.jpg",
   headerpic: "no-preview.png",
   type: "hardware,software,network",
   review: [
    {
       review_id: 3,
       org_id: 17,
       user_id: 2,
       description: "Not bad",
       rating: 5,
    },
    {
       review_id: 4,
       org_id: 17,
       user_id: 2,
       description: "Good",
       rating: 3,
    }
   ]
},
{
    id: 18,
    name_org: "US company",
    picture: "default.jpg",
    headerpic: "no-preview.png",
    type: "hardware,software,network",
    rating: 0,
    review: [
    {
       review_id: 5,
       org_id: 18,
       user_id: 2,
       description: "Well",
       rating: 5,
    }
   ]
}

I want to plus every rating in each organization and divide sum of rating with the number of rating (AVG) then show AVG in each organization

methods: {
    getOrgData() {
      axios.get("api/listorgs").then(response => {
        this.ListOrgs = response.data;
      });
    }
  },
  data() {
    return {
      ListOrgs: []
    };
  },

Can you tell me about that syntax?

2 Answers 2

1

I think you could easily use a for loop here. Here's a method which could do the trick :

getAvg(organization) {
  let total = 0;
  for(var i = 0 ; i < organization.review.length; i++) {
    total += organization.review[i].rating;
  }
  let avg = total / organization.review.length;
  return avg;
}

With this method, you just have to call it in your template, for example :

<div>{{ getAvg(organization) }}</div>

Here's a fiddle I created to solve your problem : Get rating average

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, glad I helped.
1

let us say that array of objects(company) is on companies variable.

companies = [
    {
        id: 17,
        name_org: "AAA company",
        picture: "default.jpg",
        headerpic: "no-preview.png",
        type: "hardware,software,network",
        review: [
            {
                review_id: 3,
                org_id: 17,
                user_id: 2,
                description: "Not bad",
                rating: 5,
            },
            {
                review_id: 4,
                org_id: 17,
                user_id: 2,
                description: "Good",
                rating: 3,
            }
        ]
    },
    {
        id: 18,
        name_org: "US company",
        picture: "default.jpg",
        headerpic: "no-preview.png",
        type: "hardware,software,network",
        rating: 0,
        review: [
            {
                review_id: 5,
                org_id: 18,
                user_id: 2,
                description: "Well",
                rating: 5,
            }
        ]
    }
];

companies.forEach(function(company, key){
    var divideHere = 0;
    var totalRev = 0;
    company.review.forEach(function(rev, key2){
        divideHere += 1;
        totalRev += rev.rating;
    })
    var thisCompanyAveRating = totalRev / divideHere;
    companies[key].rating = thisCompanyAveRating;

    console.log(companies[key].name_org + ': ' + companies[key].rating);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.