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

Tips for moving a title sideways and adjusting the bottom section by x pixels

I am seeking to accomplish a design similar to this using CSS in order to create a title of <h1>DISCOVER <span>WEB 3</span> DIMENSIONS</h1>. How do you recommend achieving this? Thank you, I have searched on Google and asked frie ...

Is there a way to hide a button on this virtual keyboard after it has been clicked?

After creating a virtual keyboard using HTML and CSS, I am looking to implement JavaScript code that will hide a button after it is clicked. Here is the code I have tried so far: function hideButton() { var button = document.getElementById("simple_butto ...

File writing issues are plaguing NodeJS, with middleware failing to function as well

I am facing a challenge with my middleware that is supposed to write JSON data to a file once the user is logged in. However, I am encountering an issue where no data is being written to the file or displayed on the console when the function is executed. ...

After removing an item from the array, React fails to display the updated render

As a newcomer, I am struggling with a particular issue. I have implemented a delete button for each item in a list. When the button is clicked, the object in the firstItems array is successfully deleted (as confirmed by logging the array to the console), b ...

Attempting to implement usedispatch hook in combination with userefs, however, encountering issues with functionality

I've been exploring the Redux useDispatch hook lately. I created a simple app for taking notes in a todo list format. However, I am facing an issue with useDispatch as it's not working for me and I keep encountering this error: Module build faile ...

What is the best way to dynamically adjust the size of a grid of divs to perfectly fit within the boundaries of a container div without surpassing them?

Currently, I am working on a project for "The Odin Project" that involves creating a web page similar to an etch-a-sketch. I have made some progress, but I am facing a challenge with dynamically resizing a grid of divs. The issue lies with the container d ...

Two distinct sets of radio buttons with separate functionalities

I have a unique situation where I have two distinct sections that contain sets of radio buttons with different values and IDs. However, I am facing an issue as I can only select one button at a time for both sets. My requirement is to have these two sets ...

In search of a React.js/Redux OpenID library or example, particularly focused on Steam OpenID integration

Currently, I am developing a Node/Express REST + React/Redux application that requires Steam OpenID for authentication. Despite searching extensively for tutorials, libraries, or example code related to Steam OpenID (or any other OpenID), I have come up em ...

send a json response from my webpage

I have recently developed an HTML code to construct a user form where individuals can input information into various text fields. Additionally, I have included a button on the page that is meant to gather all the entered data and transform it into a json f ...

Discovering the Newest Product Updates through API Integration

I have a component that displays only the most recent product fetched from an API: const about = ({products}) => { const data = products.attributes console.log(data) return ( <div> <h1>{data.Name}</h1> ...

Are there any available npm modules for accurately tallying the total word count within a document saved in the .doc or .doc

I Need to tally the number of words in doc/docx files stored on a server using express.js. Can anyone recommend a good package for this task? ...

Tips for including a Places Autocomplete box within an InfoWindow on Google Maps

I am currently working with the Google Maps Javascript API v3 and I am facing a challenge. I want to integrate a Places Autocomplete box inside an InfoWindow that pops up when a user clicks on a marker. I have successfully created an autocomplete object a ...

Is there a way for me to showcase a collection of pictures in an array format

I am facing an issue on my react page where the data is successfully fetched from an API, but I am having trouble fetching the array of images. Below is the code snippet that I have written: import React, {Component} from 'react'; export defaul ...

Using Typescript to create a Checkbox Grid that displays pipe-delimited values and implements a custom validation rule

I am currently working with a checkbox grid that contains pairs of AccountIds (consisting of x number of digits) and file names separated by a pipe delimiter. The file names are structured to always begin with either PRC or FE followed by a varying combin ...

Is there a way to retrieve the content of a WordPress page minus the HTML tags?

$content = fetch_content(); var_dump($content); <figure class="wp-block-gallery columns-1 is-cropped"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><img src="http://www ...

Using codedeploy to deploy a Next.js application onto an AWS EC2 instance

After creating a fresh NextJS app with only basic boilerplate files and folders, I uploaded it to a CodeCommit repository. I already had a functional CodePipeline and simply switched the Source stages. However, I am encountering deployment failures during ...

Angular Material Clock Picker for 24-Hour Time Selection

Struggling to find a time picker component that supports the 24-hour format for my Angular 14 and Material UI application. Can anyone help? ...

Exploring the dynamic world of Angular2 animations paired with the

In my layout, I have a row with three columns and two buttons to toggle the visibility of the side panels: col-md-3 col-md-7 col-md-2 ------------------------------------ | | | | | left | middle panel | | ...

The script in (Nuxt.js/Vue.js) appears to only function once, becoming inactive after switching routes or refreshing the page

I'm currently in the process of transitioning my projects website to Vue.js with Nuxt.js integrated. I have been transferring all the files from the remote server to the local "static" folder. Everything seems to be functioning properly, except for t ...

Images in the Ionic app are failing to display certain asset files

Currently, I am working on an Ionic 4 app for both Android and iOS platforms. The issue I am facing is that only SVG format images are displaying in the slide menu, even though I have images in both SVG and PNG formats. public appPages = [ { ...