Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

502
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!