Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

496
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda