Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,463 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.