What are some ways to calculate the average of data values in a Vue v-simple-table?

I am working with a v-simple-table.

The "TotalAverage" value represents the total average of "ggFinalgrade".

How can I retrieve this value?

View current image

The image I want to display

The initial value is 20

calculated as (30+20+10)/3=20

The second value is 25

found by (30+20)/2=25

My code snippet:

<template><div><v-card tile>
  <v-card-title>{{ fixed.CardTitle }}</v-card-title>
  <v-simple-table><thead><tr>
        <th class="text-left" width="25%">{{ fixed.CourseSectionsName }}</th>
        <th class="text-left" width="35%">{{ fixed.GradeItemsItemname }}</th>
        <th class="text-center" width="10%">{{ fixed.QuizAttemptsTimefinish }}</th>
        <th class="text-center" width="10%">{{ fixed.QuestionAttemptsResponsesummary }}</th>
        <th class="text-center" width="10%">{{ fixed.GradeGradesFinalgrade }}</th>
        <th class="text-center" width="10%">{{ fixed.TotalAverage }}</th>
      </tr></thead><tbody>
      <tr v-for="grade in grades" :key="grade.name">
        <td class="text-left ">
          {{ grade.csName }}</td>
        <td class="text-lefe">
          <div class="my-3" v-for="group in grade.group" :key="group.name">
            <a :href="group.url" style="text-decoration:none">
              {{ group.giItemname }}</a></div></td>
        <td class="text-center">
          <div class="my-3" v-for="qzaTimefinish in grade.qzaTimefinish" :key="qzaTimefinish.name">
            {{ qzaTimefinish }}</div></td>
        <td class="text-center">
          <div class="my-3" v-for="qaResponsesummary in grade.qaResponsesummary" :key="qaResponsesummary.name">
            {{ qaResponsesummary }}</div></td>
        <td class="text-center">
          <div class="my-3 text-center" v-for="group in grade.group" :key="group.name">
            {{ group.ggFinalgrade }}</div></td>
        <td class="text-center">{{ TotalAverage }}</td>
        </tr></tbody>
</v-simple-table></v-card></div></template>
<script>
export default { data() { return {
  fixed: {
    CardTitle: "Course 1",
    CourseSectionsName: "Content",
    GradeItemsItemname: "Quiz Paper",
    QuizAttemptsTimefinish: "Date",
    QuestionAttemptsResponsesummary: "Instructor",
    GradeGradesFinalgrade: "Outcome",
    TotalAverage: "Overall Average"
  },
  grades: [
    {
      csName: "Content 1",
      group: [{
          giItemname: "Quiz 1-1",url: "",ggFinalgrade: 30},
        {
          giItemname: "Quiz 1-2",url: "",ggFinalgrade: 20},
        {
          giItemname: "Quiz 1-3",url: "",ggFinalgrade: 10}],
      qzaTimefinish: ["0913", "0913", "1415"],
      qaResponsesummary: ["Instructor 1", "Instructor 3", "Instructor 4"]},
    {
      csName: "Content 2",
      group: [{
          giItemname: "Quiz 2-1",url: "",ggFinalgrade: 30},
        {
          giItemname: "Quiz 2-2",url: "",ggFinalgrade: 20}],
      qzaTimefinish: ["0913", "1415"],
      qaResponsesummary: ["Instructor 1", "Instructor 2"]
    }]};}};</script>

Answer №1

i. start by adding the method,

ii. calculate the average next and then return it.

iii. lastly, display the totalAverage.

totalAverage(grades) {
   var avg = 0;
   for(var i = 0; i < grades.length; i++){
       avg += Number(grades[i].ggFinalgrade);
   }
   return avg / grades.length;    
},

swap {{TotalAverage }} with {{ totalAverage( grade.group) }}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to combine ng-switch and ng-if directives in Angular?

I attempted to combine the angular ng-switch and ng-if directives, but it doesn't seem to be functioning properly. Here is what I tried: <div data-ng-if = "x === 'someValue'" data-ng-switch on = "booleanValue"> <div data-ng-swit ...

Change WordPress links to parent page without altering the current URL

Having trouble with redirect rules for a single-page app within a sub-page of a Wordpress site. Despite following the provided instructions closely, issues persist. The subpages in question are custom post types for various business locations. Upon visitin ...

Is there something else I should consider while implementing onBlur?

I am currently working on implementing form validation for a React form that I have developed. The process involves using an onChange event to update the state with the value of each text field, followed by an onBlur validation function which checks the va ...

Guide to displaying API data in HTML format

This university assignment involves working on a homework project where I need to utilize a public API in HTML. So far, I have successfully called the public API to display a list of radio channels in the left menu (without adding any click functionality). ...

Utilizing jQuery or Javascript to obtain the title of the current webpage, find a specific string within it, and then apply a class to the corresponding element

Let's imagine I start with this: <title>Banana</title> and also have some navigation set up like this: <ul id="navigation"> <li> <a href=#">Banana</a> </li> </ul> Upon loading the sit ...

Analyze the JSON data retrieved from the API endpoint to determine any

I am currently attempting to utilize axios to send an API request in order to validate login credentials, but I am facing difficulties retrieving the result from the API. The MongoDB .find function successfully locates the correct row, however, I am encoun ...

What is the best way to retrieve an attribute from an object dynamically using JavaScript?

Inside my object, I have the following values: obj.resposta1 obj.resposta2 obj.resposta3 obj.resposta4 How can I access each value inside a loop like this: for ( var int = 1; int < 5; int++) Thank you, Celso ...

Error: Reading the property 'any' of an undefined object resulted in a TypeError after an update operation

After updating multiple npm packages in my application, I encountered a series of errors that I managed to resolve, except for one persistent issue! TypeError: Cannot read property 'any' of undefined at Object.<anonymous> (/home/cpt/Deskto ...

Identify this concept: a data structure that arranges all elements in an array towards the beginning

Imagine a type of data structure that allows for random access, where removing one object causes all subsequent objects to shift forward. For instance, consider an array with a length of five but only containing three items: [A,B,C,null,null] If we remo ...

Incorporating PHP in JS/jQuery AJAX for creating unique odd and even conditional layouts

My PHP code includes a loop that changes the layout of elements based on whether the $count variable is odd or even. For odd counts, an image appears on the left and text on the right. For even counts, it's the other way around. To load content dynam ...

Incorporate a background image with the JavaScript CSS property

I'm having trouble adding a background image using the Javascript CSS property in my code. When I directly add the 'url', it works fine. Could the issue be with the 'weatherImage' variable? Javascript var OpenWeatherKey = ' ...

When utilizing AJAX within a for loop, the array position may not return the correct values, even though the closure effectively binds the scope of the current value position

Is this question a duplicate of [AJAX call in for loop won't return values to correct array positions? I am following the solution by Plynx. The issue I'm facing is that the closure fails to iterate through all elements in the loop, although the ...

Can files in a directory be listed using JavaScript without the need for HTML tags?

Currently, I am exploring ways to automate an Angular application using Protractor. During this process, I encountered a situation where I needed to retrieve a list of all the files within a specific folder. In Java, I am aware that we can accomplish this ...

Tips for inserting text to the left rather than the right using Jquery

I recently came across this code snippet shared by some helpful users on stackoverflow and it has been working perfectly for me. However, I do have a couple of queries regarding its functionality. Firstly, how can I ensure that the current selected option ...

Vuejs error: Attempting to access properties of undefined

In my Vue table, I have select options. However, an error occurs when a group is not associated with a machine, which should not happen. The goal is for only "-" to appear in this case. This error is logged in the console and causes the DataTable not to di ...

Is it possible to establish multiple connections simultaneously using Stomp?

I have been utilizing the ng2-stomp-service in my Angular application. Is it advisable to set up multiple connections (not just multiple subscriptions)? In the past, I have always seen a single connection being established, with several subscriptions made ...

Issue with Angular custom directive compatibility in version 1.3.0

Check out my code snippet This code snippet is compatible with angular **1.1.3**. However, it does not function properly with 1.3.0 ...

Creating diverse options using attribute-value pairs

I am in need of creating an array that contains all possible combinations of attribute values. Here is an example of my attributes/values object: let attr = { color: ['red', 'green', 'blue'], sizes: ['sm&apo ...

Exploring an alternative perspective on successful login in angular.js

Today was the beginning of my project to convert a website to angular.js. The main page is served by index.html and includes ng-view for other views such as login and signup. Working closely with a backend developer, I have successfully integrated a rest c ...

Tips for incorporating component templates into your Nuxt application

I am currently working on creating a dynamic header using Vue within a Nuxt application. However, I have encountered a couple of challenges: 1) I am struggling to populate the template in the original DOM with the content from an external file, which was ...