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

497
Visualizações
How to show in Array.map() html files only?

I'm using this Array.map() to list files in cache:

<div data-offline></div>
    <script>
        if (navigator && navigator.serviceWorker) {
            caches.open('pages').then(function (cache) {
                cache.keys().then(function (keys) {
                    var offline = document.querySelector('[data-offline]');
                    offline.innerHTML =
                        '<ul>' +
                            keys.map(function(key) {
                                return '<li><a href="' + key.url + '">' + key.url + '</a></li>';
                            }).join('') +
                        '</ul>';
                });
            });
        }
    </script>

This is listing all kind of files in cache - html, js, css, images etc.

But I'd like to list html files only. There is a way to do it?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Yes, there is a way: In addition to map, which transforms elements with the given function, you can use filter, which returns only those elements where the given function returns true.

So you would use keys.map(f).filter(g).join('')in your example, where f is your mapping function and g is your function that determines whether a file is of type html.

See https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filter.

about 4 years ago · Juan Pablo Isaza Relatório

0

Use Array.prototype.filter to exclude files before mapping them

Updated Example

"<ul>" +
keys
  .filter(function (key) {
    return !key.url.endsWith(".html");
  })
  .map(function (key) {
    return '<li><a href="' + key.url + '">' + key.url + "</a></li>";
  })
  .join("") +
"</ul>";
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