Steps for eliminating duplicates and increasing values in a Google spreadsheet chart

I am looking to populate a Google-chart table by looping through a list of objects. However, before drawing the table, I need to check for duplicates within the list. If any duplicates are found, I want to remove them and increment the count for the unique item.

function drawTable()
    {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Teamname');
    data.addColumn('number', 'Number of resources');
    var number = 1;
    {% for k, v in teammember %}
        {% for object in object_list %}
            data.addRows([['{{ object.team.name }}', number]]);
        {% endfor %}
    {% endfor %}
    
    var table = new google.visualization.Table(document.getElementById('table-div'));

    table.draw(data, {showRowNumber: true, width: '100%', height: '100%'});
    }

The table columns represent the name of a team and the number of members associated with that team.

Within my loop, I identify all the teams. How should I proceed to handle finding duplicates, incrementing counts, and removing duplicates? An example output could be:

  • Name - Team-members
  • Junior Bobcat 3
  • Team Bobcat 4
  • Junior Coffe 4

Answer №1

It is highly recommended to utilize Google's group() method.

This method takes a populated DataTable object and performs a SQL-like GROUP BY operation, resulting in a table where rows are grouped based on specified column values.

You can load your data table without the need to search for duplicates or incrementing values.

After loading the data table, apply the group method to generate the grouped table which can then be used to create a chart.

Check out the following sample code snippet...

google.charts.load('current', {
  packages: ['table']
}).then(function () {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Teamname');
  data.addColumn('number', 'Number of resources');

  // Add data entries here...

  var dataGroup = google.visualization.data.group(
    data,
    [0],
    [{
      column: 1,
      type: 'number',
      label: data.getColumnLabel(1),
      aggregation: google.visualization.data.sum
    }]
  );

  var table = new google.visualization.Table(document.getElementById('table-div'));
  table.draw(dataGroup, {showRowNumber: true, width: '100%', height: '100%'});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="table-div"></div>

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

showing elements within an array

I have created a JavaScript array in the following way: var chosenColors= { 'Orange' : $("#Orange").val(), 'Light Blue' : $("#LightBlue").val(), 'Dark Red' : $("#DarkRed").val ...

Instructions for correctly setting up the test-ai-classifier plugin with appium for selecting UI elements

Encountered an issue on Ubuntu 20.04. Referenced the steps provided at https://github.com/testdotai/appium-classifier-plugin Installed test-ai-classifier both under the appium path and globally using npm install -g test-ai-classifier Ensured no errors f ...

Is it necessary to incorporate iOS default controls into my HTML code?

Currently in the process of developing a cordova app, I am inclined to steer clear of default popups like the spinner triggered when selecting an item <select></select> (I plan on crafting my own dropdown tailored for mobile use). Additionally, ...

What is the best way to organize columns on the front end?

A table needs to be displayed on the front end with the following code snippet: <logic:present name="searchStudent"> <table class=" tblSearchResult" border="1" cellspacing="0" cellpadding="0"&g ...

Send a property as a string, then transform the string back into JavaScript

I am in the process of creating a versatile carousel that will cater to various conditions. Therefore, I need to set the "imageQuery" on my display page as a string and then make it work as executable javascript within my carousel. The query functions pr ...

Determine if two arrays have the same object values

I have a situation where I am working with two arrays of objects. arr1 = [{ name: "John" }, { name: "Frank" }]; arr2 = [ { name: "John", age: 35 }, { name: "Frank", age: 22 }, { name: "Kate", age: 23 ...

Instructions for sketching a square at predetermined coordinates

Looking for a simple function to draw a box at specific coordinates x and y. I've searched for guides, but they all seem to provide excessive or insufficient information. Thanks in advance! ...

How can Django Rest Framework be used to add entries to multiple tables with a single request?

When adding a new Movie to the movie model and trying to include genres at the same time, I encountered an issue with adding a Genre entry that does not already exist. The response object displayed was: {'genres': ['Object with genre=Myster ...

Tips for rearranging objects within a jsPDF document to prevent vertical overlap when a table grows in size

I am new to React and Javascript. I have been struggling to find a solution to my issue while trying to create a .pdf document for a customer invoice using "jsPdf" along with its plugin "jspdf-autoTable". So far, everything is being generated correctly by ...

What is the best way to repair all the data or preserve it using my current theme?

Seeking assistance here. I am new to developing themes and have a query. Let's say, I have developed a theme myself. Now my question is, after creating menus, posts, pages, etc., when I install just the theme on another WordPress site, all the data ge ...

Allow for the inclusion of periods and spaces when coding in JavaScript

My JavaScript code checks if the username meets certain criteria. // Check the username re = /^\w+$/; if(!re.test(form.username.value)) { alert("Alert"); form.username.focus(); return false; } Currently, the script only accepts lette ...

dynamically load react icons

Is there a way to dynamically import an icon based on props received in a component and return a compatible icon? Here is the code in question: import { useEffect, useState } from 'react'; export default function Icon({ libraryName, iconName }) ...

Concealed visual revealed through Javascript within a selected textbox

Is there a way to change the visibility of an image inside a textbox? I would like the placeholder text to become invisible when I select the "search" textbox. Can we achieve the same effect with a picture inside the textbox? Here is the code: <div id ...

Variable remains unchanged by method

I am currently working on an app that utilizes the user's webcam. In case navigator.getUserMedia fails, I need to change the error variable to display the appropriate error message instead of the stream output. Since I am new to Vue, please bear with ...

Issue encountered while attempting to install cmake-js/fastcall

Having trouble installing cmake-js/fastcall: npm install fastcall Encountering the following error: ERR! OMG There is no Visual C++ compiler installed. Install Visual C++ Build Toolset or Visual Studio. .... npm ERR! Windows_NT 6.1.7601 npm ERR! argv " ...

The customization of a Wordpress theme is causing an issue: the CSS class cannot be

I have been trying to customize a free version of a popular WordPress theme called EmpowerWP. Despite searching through all the files on the website, I have been unable to find a specific CSS class that I am looking for. As a result, I am considering two ...

Adding text chips to a text field in Vuetify - A simple guide

I have successfully integrated a text field with vuetify and now I am looking to incorporate chips into it. Currently, chips are only added if the entered text matches a specific pattern (such as starting with '{' and ending with '}'). ...

What significance does it hold for Mocha's `before()` if the function passed requires parameters or not?

In one part of my code, I have a describe block with before(a) inside. The function a originally looks like this: function a() { return chai.request(app) ... .then(res => { res.blah.should.blah; return Promise.resolve(); }); ...

Gradual utilization of Quaternion

I am currently exploring methods to apply incremental rotations to a dynamically changing orientation without using slerp. Specifics: In a 3D space, I have an object with quaternion Q describing its orientation. As the object rotates, it receives updates ...

Optimal techniques for hydrating the entire body compared to hydrating specific areas

As I continue to gain expertise in Django and Tastypie, I find myself pondering best practices. Specifically, when hydrating POST data, is it better to hydrate all fields within the hydrate function or to hydrate each field individually in a separate hyd ...