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.

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,671 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;
}