Is it possible for a nested td value to trigger the creation of a new row

[ {

    "course": "Bachelor of Computer Science",
    "level": "Bachelor's Degree",
    "provider": [
        {
            "school": "Harvard University",
            "duration": "156"
        },
        {
            "school": "Yales University",
            "duration": "156"
        },
        {
            "school": "BYU-Provo",
            "duration": "208"
        }
       
    ]
}

]

I am retrieving information from an API and the format is similar to the above code snippet. I aim to display this data in a table, so I improvised with the following approach:

data.map((item) => {
     return `
   <tr class="grid-row">
   <td>${item.course}</td>
   <td>${item.level}</td>
    ${provider(item.provider)}  // Loops through the provider array 
</tr>

The current output of the table looks like:

Course Level Provider
Science BSC. Harvard University Yales University BYU-PROVO

I want to dynamically generate new rows for each individual provider under the same course and level. This way, the updated table would look like:

Course Level Provider
Science BSC. Harvard University
Science BSC. Yales University
Science BSC. BYU-Provo

Answer №1

Retrieve information from an API using the $.get() function and dynamically add rows to a table body using the append method. For instance; index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Course</th>
                <th>Level</th>
                <th>Provider</th>
            </tr>
        </thead>
        <tbody>
           
        </tbody>
    </table>
    <script src="app.js"></script>
</body>
</html>

app.js:

function generateTableContent(data){
    const {course, level, provider} = data;
    const tableBody = $('tbody');
    for (let i; i<provider.length; i++){
        const school = provider[i].school;
        tableBody.append(`
            <tr>
                <th>${course}</th>
                <td>${level}</td>
                <td>${school}</td>
            </tr>
        `)
    }
}

$.get('<url>', generateTableContent);

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

Is it possible to perform a PHP redirect after the headers have been

I am encountering an issue with a function that needs to redirect the page once a variable is set. The challenge arises from the fact that this function is located at the bottom of the PHP page. As a result, I have already displayed a significant amount ...

Display a caution when verifying a function's value instead of its return value

I've encountered a common pitfall (especially with autocomplete) while testing a function return value. There are instances when I forget to include the parentheses () at the end, causing it to always return true. class Foo { public bar(): boolea ...

Escaping an equal sign in JavaScript when using PHP

I am currently working on the following code snippet: print "<TR><TD>".$data->pass_name."</TD><TD><span id='credit'>".$data->credit_left."</span></TD><TD><input type='button' val ...

Assign a value using the select component from Material UI

I just finished creating a dropdown menu const [selectedValue, setSelectedValue] = useState(''); const handleSelectionChange = (e: any) => { //setSelectedValue(e) console.log('value', selectedValue) } .... <Fo ...

looping through the elements in a collection using a for-in loop

Issue with IE 11 Console Chrome Console Behavior When changing the word 'item' to 'anotherItem' in the loop like so: var obj = { id1: 'item 1', id2: 'item 2', id3: 'item 3' }; for (anothe ...

Creating an aesthetic website using HTML and CSS styling

Just a simple question for all the experts out there: Can someone explain the concept of the ".NET Landscape" in relation to HTML design? Appreciate the help! ...

Creating multiple thumbnails and storing them in various folders using CodeIgniter

I am looking to organize my images into different folders - one for original pictures and another for thumbnails. However, when I try to upload an image to the thumb directory, it shows up as empty. https://i.stack.imgur.com/5Fybz.png Below is the code s ...

Attempting to enter text into a text field using Selenium

Trying to post a comment on an Instagram post using Selenium in Java. The Instagram Comment box is in a textArea. How can I successfully add a comment? My attempted solution was... WebElement cmdbox = driver.findElement(By.tagName("textarea")); ...

What is the best way to insert an object at a particular position within an array containing numerous nested objects?

This is the given Object: [ { "id": "1709408412689", "name": "WB1", "children": [ { "id": "1709408412690", "n ...

Omit a certain td element from the querySelectorAll results

Greetings! I am currently working with an HTML table and I need to figure out how to exclude a specific td element from it, but I'm not sure how to go about it. Here's the HTML code: <table id="datatable-responsive" c ...

How can one effectively manage a dynamic web element ID and xpath as they evolve?

After generating an XPath for a dynamic web element, I came across two nodes that are strikingly similar. The XPath used is: //*[contains(@id, "gwt-uid-")]/span[2]. Despite my best efforts to distinguish between the two nodes, I have not been successful. A ...

Exploring Molecular Structures with ThreeJS - Understanding the Mechanics of Double Bonds

Currently working with threejs and experimenting with the example found in this link: The challenge I'm facing is related to creating double bonds between grey and red spheres in the example. While I came across some resources suggesting ways to achi ...

Mandate that users select from the available place autocomplete suggestions exclusively

I have integrated the "Places" Google API to enable address autocomplete in an input field on my website. However, since the service is limited to a specific area, I have implemented an autocomplete filter. The issue I'm facing is that users are stil ...

The Firestore query for viewing resources is currently not functioning as expected due to issues with

I'm currently working on implementing the "read" Rules from an array, following the guidelines in this blog post var db = firebase.firestore(); db.collection("_users").where("viewers", "array-contains", myUID) .get() .then((querySnapshot ...

What could be causing my Array Push not to function correctly in Vue?

I have a question regarding adding simple numbers to an array and accessing the content of another array for comparison. Here is the code snippet I am working with: checkForUpdates(curr_dataset) { var current = curr_dataset; var update = []; // in ...

Choose the current parent item using Angular's ng-selected and $index

http://plnkr.co/edit/fwwAd4bn6z2vxVN2FUL7?p=preview On the Plunker page linked above, I am trying to achieve a specific functionality. I would like the 3 dropdown lists to display values A, B, and C initially. When a 4th dropdown option is added, it shoul ...

utilize multiple submit buttons within a single form

For some reason, I am only able to access the Histogram button from the frame. All other buttons do not seem to be working properly when clicked. Below is the form that I am trying to access in the Post method: <form id="package_form" action="" method ...

JavaScript: Zooming functions correctly as expected

I struggle with math and cannot seem to get the zooming feature to work correctly. Initially, when clicked, it zooms in perfectly to the desired location. However, if you attempt to click multiple times, it fails to zoom in as intended. Below is the code ...

Navigating Through Grid and Card Content in React

My goal was to minimize the amount of code by incorporating a reusable component into my application. I am facing an issue on how to iterate through the columns and rows in Grid, Card, and Table. What would be the optimal solution for this problem? Please ...

State variables in React hooks like useState always return the previous value before

Whenever I choose a value, it seems to always display the previously selected option instead of the current one. What I really want is for the selection to update and store the current value immediately. const [postsPerPage, setPostsPerPage] = useState(1 ...