What are the steps to ensure that data retrieved from an API is accurately displayed in a table?

My current goal is to collect data from the crypto compare API and display it in a table format. Although I am able to generate the necessary elements and append them to the table body, I am facing an unusual issue. Each time I use a for loop to iterate through individual coin data, all 100 coins are displayed in a single row rather than separating each coin into its own respective row.

Interestingly, when I manually assign a number instead of iterating through the data, the table displays a single coin per row as intended.

I have attempted storing the class names in variables and then appending the data to these variable classes, but this approach did not resolve the issue.

 for (let i = 0; i < $marketCapNumber; i++) {
  $('.a').append(
    "<tr class='tableRowData'>" +
      "<td class='rank data' id = '1'> </td>" +
      "<td class='name data' id = '2'> </td>" +
      "<td class=' symbol data' id= '3'> </td>" +
      "<td class=' marketCap data' id ='4'> </td>" +
      "<td class='price data' id='5'> </td>" +
      "</tr>"
  );
}

//data from the API 
for (let j = 0; j < 100; j++) {
  // Name
  let name = data.Data[j].CoinInfo.FullName;
  // symbol e.g btc, xrp, ltc
  let symbol = data.Data[j].DISPLAY.USD.FROMSYMBOL;
  //  price
  let price = data.Data[j].DISPLAY.USD.PRICE;
  //   marketcap
  let $marcketCap = data.Data[j].DISPLAY.USD.MKTCAP;

  //   created inputs
  const $nameOfCoin = $('<p>').text(`${name}`);
  const $ticker = $('<p>').text(`${symbol}`);
  const $coinprice = $('<p>').text(`${price}`);
  const $totalMarketCap = $('<p>').text(`${$marcketCap}`);
  // assigning variable to class names
    const nameClass = $('.name');
    const symbolClass= $('.symbol');
    const priceClass = $('.price');
    const marketCapClass = $('.marketCap');
  // append to the <td>
  $(nameClass).append($nameOfCoin);
  $(symbolClass).append($ticker);
  $(priceClass).append($coinprice);
  $(marketCapClass).append($totalMarketCap);

}

Despite my efforts, the table continues to display all coins in one row instead of organizing them individually. How can I resolve this issue so that each row in the table corresponds to one specific coin's information, rather than repeating the list of coins multiple times?

Answer №1

After setting up the table, running

$(priceClass).append($coinprice);
will capture each occurrence of
<td class='price data' id = '5'> </td>
within the table and attach the value of $coinprice to all of them. If there are 5 coins in an array labeled 1 - 5, the final result should display
<td class='price data' id='5'><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p></td>
for each row in the table.

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

Automatically generate the first user on the Parse Server system

Is it feasible to programmatically create a User on Parse server without the need for sign up? More information can be found at https://github.com/parse-community/parse-server We attempted this using cloud code. var user = Parse.User(); user.setUserna ...

In a Next.js application directory, how can you retrieve the router.asPath value within a function that utilizes the Next.js navigation feature?

import { useRoute } from "next/navigation" const route = useRoute() const updateData = () => { route.replace(route.asPath); } I attempted using next/router but it was unsuccessful ...

The font size varies depending on the language being used

On a single web page, there are 3 different language words displayed: Language / 한국어 / ภาษาไทย I am interested in enlarging the Thai word (ภาษาไทย) to make it stand out. <span class="thai">ภาษาไท ...

How to maintain row highlight in jQuery datatable even after it is reloaded

I have a datatable set to reload every 10 seconds. When a user clicks on a row, it highlights, but the highlight is lost when the table reloads. Here is a condensed version of my datatable code: $(document).ready(function() { // set up datatable $(& ...

Arrange the <form:radiobuttons> in a vertical position

I'm currently utilizing Spring MVC's <form:radiobuttons> to showcase radio buttons: <form:radiobuttons path="selection" items="${arraySelection}" /> The issue I am facing is that it is displaying the radio buttons in a horizontal la ...

How to check Internet upload speed in Angular without using a backend server?

I need help uploading a file to a folder within my Angular app's directory while it is running on localhost. I have been unable to find a solution that doesn't involve using backend technologies. For instance, I simply want to upload an image fi ...

Creating a text input with a CSS gradient

Can the text color of an input be set as a gradient? I came across this method for regular static text but it doesn't seem to work for inputs: h1 { font-size: 72px; background: -webkit-linear-gradient(to right, rgb(66, 251, 227), rgb(43, 43, 2 ...

Use jQuery in MVC 5 to remove each cloned div individually starting from the last one, excluding the default div

My default setting is as shown below. https://i.stack.imgur.com/m8Hpr.png The add button functions correctly, but I am having trouble removing cloned divs. I can only remove the last one, instead of each cloned div individually starting from the last, exc ...

Preventing audio from being muted using JavaScript requires the removal of audio tags through a MutationObserver

I attempted to utilize the script below to eliminate all audio from a specific website: // ==UserScript== // @name addicto // @namespace nms // @include http://* // @include https://* // @version 1 // @grant none // ==/UserScrip ...

Ways to address time discrepancies when the countdown skips ahead with each button click (or initiate a countdown reset upon each click)

Every time I click my countdown button, the timer runs normally once. But if I keep clicking it multiple times, it starts skipping time. Here are my codes: <input type="submit" value="Countdown" id="countdown" onclick="countdown_init()" /> <div i ...

I am a beginner in the world of Typescript/Angular, and I have encountered a compiling error TS2339. It seems that even when the condition *ngIf="x.length > 0;" should be false, the

I'm currently enrolled in a Typescript/Angular course where I am learning about the implementation of "*ngIf". During one of the lessons, the instructor provided an example using an empty array to demonstrate how it fails the condition and results in ...

Utilizing custom links for AJAX to showcase targeted pages: a beginner's guide

I am putting the final touches on my website and realized that it may be difficult to promote specific sections of the site because the browser address never changes. When visitors click on links, they open in a div using Ajax coding. However, if I want to ...

Switch out the ajax data in the input field

Is there a way to update the value in a text box using Ajax? Below is my code snippet: <input type="text" id="category_name" name="category_name" value="<?php if(isset($compName)) { echo ucfirst($compName); ...

Finished drawing remains on the iPad canvas within the browser

When it comes to clearing sketches on a canvas, I am experiencing some confusion. Despite successfully clearing the sketch from the canvas, as soon as I click to draw something new, the cleared sketch reappears in its original position. It seems like the ...

Implementing an API call in Vue JS on the app.vue component of a single page application

My project is experiencing delays in API requests due to a large amount of data. I have tried adding a cache, but the page still appears white upon creation. I am considering moving the API call to app.vue to speed up the request. Is there a way to do this ...

What is the best way to retrieve localstorage information within the getStaticProps function in next.js?

Having trouble retrieving local storage data with the following code. localData = { id: localStorage.getItem("id"), token: localStorage.getItem("token"), }; This snippet is housed within a function called getStaticProps ...

When clicking on the register button in Node.js, no activity is initiated

Once upon a time, there was a humble express.js app that needed to establish a connection with a mysql database and retrieve user information for registration within the said database. Despite successfully connecting to the database, nothing would happen w ...

How to Retrieve Content from an iFrame Using jQuery?

I am facing a challenge with an HTML form that is directed to an IFRAME upon clicking "submit". The iframe then loads a page after the submission, which indicates whether the user-input data is valid or not. It simply displays on screen as "TRUE" or "FALSE ...

Change HTML table information into an array with the help of JavaScript

I am facing an issue where the array is displaying as undefined even though I am storing table data in an array. Check out more about arrays here. Attempting to retrieve each problem's row in the form of an array function getAllProblemRowElements() ...

Exploring AngularJS through interactive sessions

I am working on implementing a basic login system using express and angularjs. The angular js application is running on a different server (grunt server localhost:9000), while the express app is running on a separate port. In my express app, I have configu ...