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

APIs Displaying the Content

I think the api is updated for all breeds objects

The api for all breeds is updated and now the function to add the options in select menu is broken. If someone can have a look and update the code please.

3 Answers

Lewis Marshall
Lewis Marshall
22,673 Points

This API endpoint still works fine https://dog.ceo/api/breeds/list but I couldn't find it on the Dog API docs.

The new api url for all breeds is: https://dog.ceo/api/breeds/list/all I have got all breed object using the following code:

fetch("https://dog.ceo/api/breeds/list/all")
    .then(response => response.json())
    .then(data => {
        const dataMessage = data.message;
        const messageKeys = Object.keys(dataMessage);
        generationOptions(messageKeys);
    });

after that the generateOptions function:

function generationOptions(data) {
    let options = "";
    data.map(item => {
        options += `
        <option value='${item}'>${item}</option>`;
    });
    select.innerHTML = options;
}

Sure, they will be many better ways, but thats I could come up with at the moment. Hope its helpful if anyone else had the same issue recently.

The API does indeed seem to be updated with a nested structure that includes sub-breeds for the all breeds list. The following code solves the problem:

function generateOptions(data) {
  let breedsList = [];
  let html = '';
  Object.keys(data).forEach(breed => {
    if (data[breed].length === 0) {
      breedsList.push(breed);
    } else {
    data[breed].forEach(subBreed => {
      breedsList.push(`${breed}/${subBreed}`)
    })
    }
  })
  breedsList.forEach(breed => {
    let readableBreed;
    if (breed.includes('/')) {
      readableBreed = `${breed.split('/')[1]} ${breed.split('/')[0]}`;
    } else {
      readableBreed = breed;
    }
    html += `<option value=${breed}>${readableBreed}</option>`
  })
  select.innerHTML = html;
}