Traversing JSON Data using Vanilla JavaScript to dynamically fill a specified amount of articles in an HTML page

Here is the code along with my explanation and questions:

I'm utilizing myjson.com to create 12 'results'. These results represent 12 clients, each with different sets of data. For instance,

Client 1: First Name - James,

Address - 1234 Maple Street

Client 2: First Name - Jack,

Address - 4321 Lorem Ipsum Lane

My Query: How can I populate the following 12 articles in HTML using the JSON Data with a For Loop in my JavaScript and Ajax Request?

    <article>
        <img class="photo" height="100" width="100">
        <hgroup>
            <h1 class="name"></h1>
            <h2 class="email"></h2>
        </hgroup>
    </article>

    <!-- Repeat this article structure 11 more times -->

`const Employees = (function () { let displayStudent = document.querySelector('.photo'); let name = document.querySelector('.name'); let email = document.querySelector('.email'); let phone = document.querySelector('.phone'); let streetAddress = document.querySelector('.streetAddress'); let city = document.querySelector('.city'); let state = document.querySelector('.state'); let zip = document.querySelector('.zip'); const ajaxCall = function () { let hr = new XMLHttpRequest(); let url = 'https://api.myjson.com/bins/zfhmr'; hr.onreadystatechange = function () { if (hr.readyState === 4) { if (hr.status === 200) { let myObj = JSON.parse(hr.responseText); for(let i = 0; i < myObj.length; i++) { displayStudent.src = myObj.results[i].picture.large; name.innerHTML = myObj.results[i].name.first + " " + myObj.results[i].name.last; } } } else { console.log("ajax error: " + hr.response); } }; hr.open("GET", url, true); hr.send(); }; return { init: function () { ajaxCall(); } }; }()); Employees.init();`

I'm struggling to populate more than one article at a time with just one client's information. Any assistance would be highly appreciated!

Thank you

Answer №1

Upon examining your code example and comparing it to the JSON response, I noticed something peculiar.

This is what your JSON response looks like...

{
    "results": [...]
}

The for loop in your code appears as follows...

let myObj = JSON.parse(hr.responseText);
for(let i = 0; i < myObj.length; i++) {
    displayStudent.src = myObj.results[0].picture.large;
    name.innerHTML = myObj.results[0].name.first + " " + 
    myObj.results[0].name.last;
}
  • It seems that you are attempting to iterate through an object that does not have a .length property, indicating that myObj is not an array.

  • Furthermore, myObj.results[0] consistently retrieves the first result.


Possible Solution

In order to address this issue, you may want to dynamically insert HTML elements within your for loop.

<!-- Inside your HTML file -->
<div id="articleContainer"></div>

// Within your JavaScript ajax response (upon success)
if (hr.readyState === 4 && hr.status === 200) { // confirms completion of ajax request
    let container = document.querySelector('#articleContainer');
    let strArticles = "";

    let myObj = JSON.parse(hr.responseText);
    for(let i = 0; i < myObj.results.length; i++) {
        let resObj = myObj.results[i];

        strArticles += '<article>' +
                            '<img class="photo" src="' + resObj.picture.large + '" height="100" width="100">' +
                            '<hgroup>' +
                                '<h1 class="name">' + resObj.name.first + ' ' + name.last + '</h1>' +
                                '<h2 class="email">' + resObj.email + '</h2>' +
                            '</hgroup>' +
                        '</article>';
    }

    // Integrating the generated HTML string into the designated parent container element.
    container.innerHTML = strArticles;
}

I hope this resolves the issue for you :)

Answer №2

When working with variables in a for loop, it's important to ensure they are within the scope of the loop and avoid nesting if statements. If you need to populate HTML snippets with data, using a template can be more efficient than querying individual elements with selectors.

const Employees = (function() {
  let displayStudent = document.querySelector('.photo');
  let name = document.querySelector('.name');
  let email = document.querySelector('.email');
  let phone = document.querySelector('.phone');
  let streetAddress = document.querySelector('.streetAddress');
  let city = document.querySelector('.city');
  let state = document.querySelector('.state');
  let zip = document.querySelector('.zip');

  const ajaxCall = function() {
    let hr = new XMLHttpRequest();
    let url = 'https://api.myjson.com/bins/zfhmr'; //https://randomuser.me/api/
    
    hr.onreadystatechange = function() {
      if (hr.readyState === 4 && hr.status === 200) { 
        let myObj = JSON.parse(hr.responseText);
        for (let i = 0; i < myObj.length; i++) {
          displayStudent.src = myObj.results[0].picture.large;
          name.innerHTML = myObj.results[0].name.first + " " + myObj.results[0].name.last;
          
        }
      } else {
        console.log("ajax error: " + hr.response);
      }
    };

    hr.open("GET", url, true);
    hr.send();

    console.log(hr);


  };
  return {
    init: function() {
      ajaxCall();
    }
  };

})();

Employees.init();
<article>
  <img class="photo" height="100" width="100">
  <hgroup>
    <h1 class="name"></h1>
    <h2 class="email"></h2>
  </hgroup>
</article>

// Repeat this article snippet as needed

An example:

// Performing an AJAX call or fetch to retrieve data

var data = [{
  email: 'something'
}, {
  email: 'something2'
}, {
  email: 'blah'
}, {
  email: 'blah2'
}, {
  email: 'blah blah'
}, {
  email: ':o blah'
}]


data.forEach(item => {
  var div = document.createElement('div')
  div.innerHTML = `<div>some fancy box////   ${item.email}   \\\\\\some more facy box</div>`

  document.body.appendChild(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

"When attempting to render a Node inside the render() method in React, the error message 'Objects are not valid as a React child' is

On my webpage, I have managed to display the following: export class OverworldComponent extends React.Component<OverworldComponentProps, {}> { render() { return <b>Hello, world!</b> } } However, instead of showing Hello, ...

Is there a way to trigger the opening of a new file or page when a CSS animation comes to an end?

Is there a way to delay the loading of a function or page until after an animation has finished running in JavaScript, HTML, and CSS only? For instance, I'd like to run an animation first and then have a different website or content load afterwards fo ...

Having trouble retrieving all JSON properties

I am facing an issue with my json structure where I am unable to access certain properties. I can only access the main properties like type, properties, and so on within that hierarchy level. However, I cannot seem to access icon, iconURL, or title. The da ...

Why does it display 'Undefined' when I attempt to access an object in an array in Next.js?

I am currently working on a way to display sub Products related to the main product. I have attempted to extract the product code from the URL and then retrieve sub-products that correspond to the main product code. List of Products and Sub products const ...

ReactJS is encountering a situation where two children are using the same key and causing

When I try to view the profile information of another user, I encounter a duplicate key error that says: "Warning: Encountered two children with the same key, ``. Keys should be unique so that components maintain their identity across updates. Non-unique k ...

Vue: Storing selected list values in an array

I am working on a Vue application where I need to select two elements from a list component and place them inside an array. Currently, I have my list set up with selection functionality thanks to Vuetify. I have bound the selected items to an array using v ...

Is there a disconnect between the input and upload methods?

While developing a website using PHP and Ajax, I encountered an issue where I need to use the GET method for inputting data when performing certain actions but have to utilize the POST method for uploading images due to its limitations. How can I retrieve ...

Exploring the intricacies of JSON object retrieval

I'm currently working on a form that allows users to submit address details for a selected location. However, before submitting the form, I want to give the user the ability to preview the address that will be sent. The addresses are stored within a J ...

Validation scheme for the <speak> element

When using validators in an angular formarray for input fields, I encountered a challenge with the regex to check the <speak> tag. The content provided was considered valid. An error is thrown based on the specified pattern. However, it should als ...

How to Maintain Button Activation in NextJS

I am facing an issue with my buttons and form interaction. I have a group of 3 buttons and a form. When I select a button, it stays active as expected. However, once I click on the "submit" button in the form, the previously selected button loses its activ ...

The alignment of flexbox within a list item is not positioned at the top as expected

When I place a flexbox inside a list item, the content is being pushed down by what seems to be a full line height. I have tried various CSS properties like setting margin-top: 0 for the flexbox, margin-top: 0 for its children, adding flex-wrap to the fle ...

Separate the two divs with some added spacing

I am facing a challenge in creating a navbar with two regions split by white space. I attempted to use float:right and justify-content: space-between, but it doesn't seem to work as expected. My goal is to have site-header-right on the right side and ...

Utilizing the Ajax Tool Kit for Autocomplete Dropdowns in ASP .Net

Currently, I am utilizing an Ajaxtool kit AutoComplete extender control that is linked to a Text box. By calling a web service, I am able to fetch values to bind to the AutoComplete extender, and it is functioning perfectly. The issue arises when a user s ...

Utilizing tags within the pre tag to incorporate additional styles

Using pre tags to display code works well, but I need to style certain parts of the code in green. However, when I wrap the desired code in a <div> tag with styling, it affects formatting by adding newlines and swallowing spacing. How can I achieve t ...

"Exploring the use of conditional rendering in React to dynamically hide and show components based

I am currently immersed in the world of React/Redux, focusing on an e-commerce project. This particular application offers two payment methods: cash and card payments. On the product display page, both payment icons are visible. However, I am seeking a sol ...

Encountering an error that states "this.push is not a function" when trying to select the 2nd or 3rd option

My goal is to extract elements from a drop-down menu and populate a list box based on the selection. The selected value from one list box should then be transferred to another list box, specifically from Available to Selected. However, while selecting Deve ...

Passing variables from AJAX response to PHP using a passthru function

In my PHP file, I have implemented a functionality where certain checkboxes are checked and upon clicking a button, a JavaScript file is triggered. The JavaScript code snippet used is as follows: $.ajax ({ type: "POST", url: "decisionExec.php", ...

When a class and ID are dynamically applied, the click event may not fire and the CSS may not be applied

Hey everyone, I am facing an issue with declaring id and class when creating a table dynamically. I have tried multiple functions to make my click event work but without success. Can anyone help me with this? Below is the code for my dynamic table where I ...

Bidirectional binding with complex objects

In my Angular2 app, I have a class called MyClass with the following structure: export class MyClass { name: Object; } The name object is used to load the current language dynamically. Currently, for two-way binding, I am initializing it like this: it ...

Steps to restrict input in a text area to only backspace and cursor movements

I'm in search of a jQuery function that restricts movements to only arrow keys and backspace within a textarea. However, there seems to be an issue with the arrow key movements not functioning correctly. function moveArrow(e){ if(e.which >= 3 ...