Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
Ordenar datos con fetch

Necesito tu ayuda. Con la ayuda de fetch, obtengo los datos e inmediatamente los dibujo. En el botón quiero que se ordenen estos datos, sin embargo recibo un error. ¿Qué estoy haciendo mal?

 <button id="sort_by_name">Sort</button>

JavaScript

 fetch(`https://api.sampleapis.com/wines/reds`) .then(response => response.json()) .then(data => { for (let i = 0; i <= 25; i++) { let vino = data[i] let a = document.getElementById('sort_by_name'); a.addEventListener('click', () => { data.sort((a,b) => a.wine - b.wine) }) document.write(`<div>${vino.wine}</div>`) } })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. Agregue un detector de clics al botón.
  2. Obtenga los datos y guárdelos en una variable.
  3. Mostrar los datos. La forma más sencilla posible es usar .innerHTML , pero también puede aprender a usar .appendChild .
  4. Para ordenar la lista, use .localeCompare .

 let wineList; document.getElementById('sort_by_name').addEventListener('click', sortList); // 1 fetch(`https://api.sampleapis.com/wines/reds`) .then(response => response.json()) .then(data => { if (data) { wineList = data; // 2 display(wineList); // 3 } }) function display(list) { // 3 const wineDataEl = document.getElementById('wine_data'); let htmlStr = ''; for (const wine of list) { htmlStr += wine.wine + '<br/>'; } wineDataEl.innerHTML = htmlStr; } function sortList() { // 4 if (wineList) { wineList.sort((a, b) => a.wine.localeCompare(b.wine)) ; display(wineList); // 3 } }
 <button id="sort_by_name">Sort</button> <div id="wine_data">Loading...</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda