Arrange JSON information in an HTML table with distinct header rows for every data category

I have a JSON object with a key:value pair named callRoot. Some examples of values for this pair are @S, @C, and @W. There are multiple objects that share the same value and I want to create an HTML table head row at the beginning of each group and repeat it for each data set.

Latest Update

Here's a Fetch API function for accessing remote API data:

const fetchData = function() {
  const apiKey = "";
  const request = `https://apiurl.com`;

  fetch(request)
    .then(response => response.json())
    .then((data) => foo(data))
    .catch(error => {
      console.log(error);
    });
  }
fetchData();

New Update

for (let i of filtered[property]) {
  const tr = document.createElement('tr');
  tblBody.appendChild(tr);
  data.forEach(element => {
    console.log(element);
  });

Sample JSON data provided below:

const data = [
  {
    "symbol": {
      "description": "SOYBEANS January 2022"
    },
  // more data...

];

Code snippet:

// Create empty arrays for each callRoot grouping

// further implementation is similar to above code block...

Example structure of the HTML table:

<table>
  <tbody>
    <tr>
      <th colspan="9">SOYBEANS</th>
    </tr>
    // more table rows...
  </tbody>
  // repeated structure for other groups...
</table>

https://i.sstatic.net/aptmw.png

Answer №1

I created a function that generates a table based on the example provided.

Below is the script/function I used:

OLD Function with Incorrect Data but Correct Output

const filterData = function (data) {
    const filtered = {};
    
    data.forEach(element => {
        if (filtered[element.callRoot]) 
            filtered[element.callRoot].push(element);
        else 
            filtered[element.callRoot] = [element];
    })
    
    return filtered;
};

// More code goes here...

FALSE OLD Data Used:

const commodities = [
    // Data here...
];

Output Using RIGHT Example Data:

<div>
    <table>
        // Table content generated from the correct example data
    </table>
</div>

New Solution Approach:

New Version of the Script Provided Below:

const filterData2 = function (data) {
    // New filtering logic for different structured data
};

// More updated script here...

Data for the New Example:

const data = [
    // New example data to be used in the updated script
];

Output with the Updated Example Data:

<div>
    <table>
        // Table content generated using the new example data source
    </table>
</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

What is the best way to fetch images from a JSON object in a React application?

I have been trying to load images from an array of objects in React, but I keep encountering a "module not found" error. This issue has been frustrating me for the past couple of days. Can someone please help me troubleshoot this problem or provide some su ...

Using Node.js to seamlessly read and upload files with Dropbox API

I am utilizing the Dropbox API to retrieve a file from the node.js file system and then transfer it into a designated Dropbox folder. The transfer process is successful, but the file ends up being empty, with a size of 0 bytes. var path = require("path") ...

Error with JSON parsing caused by commas in JSON file using JsonSlurper

When JSON data is received in my controller as params.formData, it has the following structure: '{"year":"2014","resource":["Smith, John","Foo, Bar"]}' Here's the code snippet I use to parse this data: .... def slurper = new JsonSlurper() ...

Engage in a conversation with a specific individual on the internet using node.js

Looking to implement a chat feature with specific online users similar to Facebook or Gmail using node.js and socket.io. Can anyone assist me with this? Thanks in advance! Client.html <html> <head> <title>My Chat App</title> <d ...

Angular: Array in the template re-renders even if its length remains unchanged

Below is the template of a parent component: <ng-container *ngFor="let set of timeSet; index as i"> <time-shift-input *ngIf="enabled" [ngClass]="{ 'mini-times' : miniTimes, 'field field-last&ap ...

Applying binary information to an image

Let's say I have an <img/>. The img is initially set with src='http://somelocation/getmypic'. Later on, there might be a need to change the content of the image based on some ajax call that returns binary data. However, this decision c ...

What is the best way to declare a variable within a VueJS v-if condition without it being visible?

In my code, I have a specific template that is displayed when a certain condition is met using a v-if statement. In this template, I need to set a variable to N/A, but I am struggling with how to accomplish this simple task. <span v-if="se ...

"What is the process for developing a Web-component with Vue and typescript that can be used in

Utilizing vue-custom-element, I have successfully created a Web component in Vue and integrated it into Angular. This setup operates seamlessly for Vue+js: import Vue from 'vue' import Calculator from './components/Calculator.vue' impo ...

In Typescript, if at least one element in an array is not empty, the function should return false without utilizing iterators

My current approach involves receiving a string array and returning false if any of the elements in the array is false. myMethod(attrs: Array<String>) { for (const element of attrs) { if (!element) { return false; } } ...

Issue with vue-apollo package causing GraphQL query not to display on the frontend

Currently, I am expanding my knowledge of GraphQL and working on a project where I aim to display queries in the front end. To achieve this, I have incorporated the package GitHub - Akryum/vue-apollo: ...

Limit the rotation of jQuery and CSS3 elements to always turn in a clockwise direction

Utilizing the jQuery transit plugin, I am rotating a block element with 4 navigation controls. Each time a nav item is clicked, the block rotates to the specified custom data attribute of that nav item - either 0, 90, 180, or 270 degrees. While I have suc ...

What is the reason behind TypeScript indicating that `'string' cannot be assigned to the type 'RequestMode'`?

I'm attempting to utilize the Fetch API in TypeScript, but I keep encountering an issue The error message reads: Type 'string' is not assignable to type 'RequestMode'. Below is the code snippet causing the problem export class ...

Aligning the stars with CSS

One of the components I have deals with a star rating system, and I thought it would be cool to use Font Awesome icons for half stars. Everything is working well except for the CSS styling aspect. While I managed to position some of the stars correctly by ...

Modify the state of an individual item within an array

I have a list of items that I need to show a loader for and hide it once a certain action is completed. For instance, below is my array of items: [ { "id": "69f8f183-b057-4db5-8c87-3020168307c5", "loading": null } ...

Why is my website returning a 404 error while attempting to retrieve a JSONObject?

@Override protected JSONArray doInBackground(Long... ids) { id=ids[0]; apiURL="http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=534FA68616FC166C4A9F11CEB01B9929&steamid=76561197976528744&format=json" ...

The show-word-limit feature is malfunctioning on the element.eleme.io platform

After attempting to utilize the show-word-limit attribute of the input element, unfortunately the character count did not display. Below is the code snippet that was used: <el-input type="textarea" placeholder="Please input" ...

What is the best way to convert a graphql query into a JSON object?

I'm facing an issue where I need to convert a GraphQL query into a JSON object. Essentially, I have a query structured like the example below, and I'm seeking a method to obtain a JSON representation of this query. Despite my efforts in searching ...

How to Send and Receive Data in One Request Using Android Studio and Web API

Currently, I am working on an Android App using Android Studio and the backend is being developed using Asp.Net Web API. Web Api Part [System.Web.Http.AcceptVerbs("GET", "POST")] [System.Web.Http.HttpGet] public dynamic GetTest(string name) { List< ...

Step-by-step guide to implementing a user-friendly search input field using the powerful AngularJS material design framework

I am searching for an effortless method to implement a feature similar to the expandable search text field in angular-mdl. By clicking on a search button, it will expand into a text field. <!-- Expandable Textfield --> <form action="#"> < ...

Retrieving a specific attribute pair from a JSON object

Currently, I am trying to retrieve the temperature data and display it on my webpage. Since these are objects with no specific order, I am struggling to understand how to access them without using an index. { "response": { "version": "0.1", "termsofServic ...