Multi-dimensional array: Include an extra array if the desired value is not located

I am currently working on a project that involves using Google tables to create a report based on data retrieved from my MYSQL Database.

The issue I'm encountering is that there are 5 header values:

['Call Disposition', 'Answered', 'Busy', 'No Answer','Failed']

The problem arises when the data from the database only includes 3 out of the 4 values needed to define the Google chart header. If "Failed" is missing and only one other value is present, Google Charts throws an error:

Uncaught Error: Row 0 has 4 columns, but must have 5

For clarity, the values in the database are automatically inserted based on call outcomes, with not all calls resulting in failure.

Only a few instances will have the "Failed" value, so I am exploring ways to dynamically add the missing array if it's not present for a particular number, setting the missing value to 0.

For example, this is the current format based on data pulled from the database and placed into an MD Array:

[ [ 'ANSWERED', '477', 728 ],
  [ 'BUSY', '477', 48 ],
  [ 'NO ANSWER', '477', 277 ],
  [ 'ANSWERED', '488', 953 ],
  [ 'BUSY', '488', 9 ],
  [ 'FAILED', '488', 1 ],
  [ 'NO ANSWER', '488', 126 ] ]

The code I am currently using is as follows:

 // code block here
   

This code interacts with the database and processes the results to ensure all necessary values are included in the array before generating the report using Google Charts on the client side.

Is it possible to automatically add missing values in Google Charts? I'm not certain if this functionality is available, so I'm exploring alternative solutions to address this issue.

Answer №1

Assuming the array needs to have a similar structure to the following...

['Call Result', 'Success', 'Busy', 'No Response', 'Failure']

It might be more convenient to modify the SQL query as shown below...

select
  a.source,
  (select count(*) from calls where source = a.source and result = 'SUCCESS'),
  (select count(*) from calls where source = a.source and result = 'BUSY'),
  (select count(*) from calls where source = a.source and result = 'NO RESPONSE'),
  (select count(*) from calls where source = a.source and result = 'FAILURE')
from
  calls a
where
  a.source in ("123456", "789012")
group by
  a.source

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

How to automatically reset a form after submission in React using Ant Design?

When using the antd template for form design, I encountered an issue where form input values were not getting cleared after submission. I attempted to use this.props.form.resetFields(), but it resulted in the following error: Unhandled Rejection (TypeErro ...

What is the best way to isolate a single element within a for loop and exclude all others?

I have implemented a dynamic Ajax call to compare the string entered in the text field (representing a city name) with the "type" value in a JSON array. As I iterate through the elements of the array, I am checking the values associated with the key "type ...

Checking values in an array in Codeigniter

Here is an example array: $data=array( 'a'=>'value1', 'b'=>'value2', 'c'=>'value3', 'd'=>array('e'=>'value4','f'=>'value5' ), ...

Guide on invoking a POST endpoint within another POST API in a Node.js Express server

I encountered an issue while attempting to use fetch in react.js with a backend node.js API URL, which then proceeds to make a POST API call within the server to another route utilizing a different URL. How can I tackle this task effectively? See the code ...

Trimming a Three.js Sprite to conform with the boundaries of its parent Object

Imagine a scenario where I have a Sprite, which was created and added as a child of an Object with a transparent material, like so: let mySprite = new THREE.Sprite(new SpriteMaterial({ map: myTexture })); mySprite.scale.set(2, 2, 1.0); mySprite.posit ...

What is the best method for asynchronously injecting and providing data?

Within my page, I have implemented an asynchronous fetch method to initialize data: async fetch() { const res = await requestApi(this, '/database'); this.sliderData = res.homeSlider; this.modelData = res.model; ... } To pass thi ...

Ways to cease a setInterval after a specified duration or a specific number of iterations

I have successfully implemented a jQuery code for creating a loop of "changing words" by referring to the solution provided in this Stack Overflow answer: jQuery: Find word and change every few seconds My question is, how can I stop this loop after a cert ...

Tips on how to modify particular information within a column using SQL when following a specific pattern

In my database, there is a table column containing data with absolute URLs, such as: +---------------------------------+ | url | +---------------------------------+ | /foo/12 | | http://www.myexample.com ...

A duo creating art on a shared canvas

I have encountered an issue while developing a real-time paint app using HTML5 canvas. Everything works fine when a single user is drawing on the canvas, but problems arise when two users try to draw simultaneously. For example, if one user changes the col ...

The ng-click event for the reset button is limited to a single use

There seems to be a problem with the reset button functionality on my webpage. Although it initially works, it only works once and then requires a reload of the page to function again. Here is the JS code: var ctrl = this; var original_device = angular.c ...

Craft an Interactive Content Carousel

I have been searching for a responsive slide solution for two days with no luck, so I am reaching out for help. I am looking for a simple jQuery code for a slideshow that is lightweight and doesn't require a plugin. Here is the code for the slideshow: ...

Dynamically parsing JSON data with jQuery

I have some JSON data that looks like this: { "default": [ [ 1325876000000, 0 ], [ 1325876000000, 0 ], [ 1325876000000, 0 ], [ 1325876000000, 0 ] ], "direct": [ [ ...

Combine React 17 with webpack 5 and babel-plugin-react-css-modules, enabling the use of CSS modules with stylename functionality

I am in the process of setting up a new React application using the latest technologies available, including React 17, Webpack 5, and other essential modules. My goal is to implement CSS modules utilizing the styleName concept with the help of babel-plugi ...

Show the HTML page returned by the server on the client side

Hey there! I've come across a PHP code snippet that sends back an HTML file as a response. This PHP code makes use of the include method to send the file. When I'm on the client side, I'm employing AJAX. Upon typing console.log(data) (with ...

Issue with running the Jquery each function within a textbox inside an ASP.NET gridview

Below is the gridview markup: <asp:GridView ID="gvDoctorVisits" runat="server" DataKeyNames="AdmissionId" class="tableStyle" AutoGenerateColumns="False" Width="100%" EmptyDataText=& ...

Disable the ability to close the dialog box by clicking

this is my dialog <div *ngIf="visible" class="overlay" (click)="close()"> <div role="dialog" class="overlay-content"> <div class="modal-dialog" (click)="$event.stopPropagation()"> <!-- Modal content--> ...

Guide on retrieving the response headers with jQuery and AJAX

Connection: keep-alive Content-Length: 2231 Content-Type: text/html; charset=utf-8 Date: Thu, 13 Sep 2018 07:37:46 GMT ETag: W/"8b7-XOXhf04O/VM7yxWQ561PEgxRfz8" x-auth: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YjlhMGEyM2Q0NmI3YjFmYTQzNWI ...

What could be causing Map() to not display itemlist, even though the items are being rendered separately in ReactJS?

Recently, I encountered a problem while working on an app using ReactJS, Redux, and Firebase/Firestore. The issue arises when I fetch data from Firestore and store it in the ourPosts variable using useState([]) initialization. The strange thing is that wh ...

Showing formatted JSON (Large Object) on an HTML page

Having trouble displaying formatted JSON in an HTML page, especially when dealing with larger objects (an array of objects). The current method involves using JSON.stringify(data) to display the JSON object upon receiving a response from the server. The P ...

Adding Hash to Array: final Hash replaces existing array elements

Here is a Ruby script I am working with: arr = ['bob', 'jack', 'smith'] array_of_hashes = Array.new hash = Hash.new arr.each do |item| hash.clear hash[:name] = item array_of_hashes << hash end puts array_of_hash ...