Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) jQuery and AJAX The Office Status Project Revisited

My solution using ES6

This is just what I came up with before watching the video. Really enjoying AJAX!

$.getJSON('data/employees.json',(response) => {
  let html = '<ul class="bulleted">';
  $.each(response,(index,value) => {
    let name = value.name;
    let isOutOfOffice = value.inoffice ? 'in' : 'out';
    html += `<li class="${isOutOfOffice}">${name}</li>`;
  });
  html += '</ul>';
  $('#employeeList').html(html);
});

1 Answer

Angelica Hart Lindh
Angelica Hart Lindh
19,465 Points

Hi Derrick,

Great work using ES6! It's brought a lot of improvements to JavaScript in my opinion.

I wanted to show you a "jQueryLESS" version in case there comes a time where you work on a project that isn't using jQuery.

fetch('data/employees.json')
   .then(response => response.json()) // Convert the response blob to JSON
   .then((data) => { // Data is now the JSON object
      let html = '<ul class="bulleted">';
      data.forEach((item) => {
         const isOutOfOffice = item.inoffice ? 'in' : 'out';
         html += `<li class="${isOutOfOffice}">${item.name}</li>`;
      })
      html += '</ul>';
      document.getElementById('employeeList').innerHTML(html)
   })
   .catch(error => console.error(error)); // Catch any errors on the fetch Promise response;

The Fetch API was used instead of jQuery getJSON. Also you can make use of the built in Array method forEach for iterating over an Array.