我想通过使用 filter 函数来过滤 displayConcertsfetch 的结果 . 我无法在 displayConcerts 中获取 filt (来自文档的输入)来过滤 concert.eventDateName .

因此,如果输入是"h",它将仅显示 concerteventDateName 以"h"开头 .

(function () {

    function createNode(element) {
        return document.createElement(element);
    }

    function append(parent, el) {
        return parent.appendChild(el);
    }

    const ul = document.getElementById('concerts');
    const url = 'https://apis.is/concerts';
    let search = document.getElementById('search');
    //-----------------------------------------------------------

    const getConcerts = () => {//get the concert data and output in console
        return fetch(url)
        .then(res => res.json())
        .then(concerts => console.log(concerts.results))
    }


    let displayConcerts = (query) => {//displays concerts in html file
        let filt = query;
        //console.log(filt);
        fetch(url)
        .then((resp) => resp.json())
        .then(function(data) {
            let concerts = data.results;
            return concerts.map(function(concert) {//   outputs images and text
                if (concert.eventDateName) {
                    let div = createNode('div'),
                    img = createNode('img'),
                    span = createNode('span');
                    img.src = concert.imageSource;
                    span.innerHTML = `${concert.eventDateName}`;
                    append(div, img);
                    append(div, span);
                    append(ul, div);
                }
            })
        })
        .catch(function(error) {
            console.log(error);
        });
    }

    //-----Filter for concert search------------------------------

    function filter() {
        let query = this.value.trim().toLowerCase();
        //console.log(query);
        getConcerts();
        displayConcerts(query);
    }





    search.addEventListener("input", filter);
    search.addEventListener("keyup", filter);

}());