简体   繁体   English

如何更改此 function 以显示一个结果?

[英]How to change this function to display one result?

I have this function that shows all matches that have "20 pack" in the json database.我有这个 function,它显示了 json 数据库中所有具有“20 包”的匹配项。 Is there a way to manipulate this function to just search and stop after the first match.有没有办法操纵这个 function 在第一次匹配后搜索并停止。 The database is in price ascending order hence the first result will be the cheapest.数据库按价格升序排列,因此第一个结果将是最便宜的。

 function displayMatches() {
    const searchText = '20 pack';
    const searchUrl = document.querySelector('.custom-select').value;
    const matchArray = findMatches(searchText, searchUrl, name);

      const html = matchArray.map(place => {
        const regex = new RegExp(searchText);
        const nameName = place.name.replace(regex, `<span class="hl">${searchText}</span>`);
             var shop = place.url.replace(/(centra)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase());
             var shop1 = place.url.replace(/(tesco)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase());
             var shop2 = place.url.replace(/(aldi)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase());
             var shop3 = place.url.replace(/(lidl)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase());
             var shop4 = place.url.replace(/(supervalu)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase());
        return `
            <a href="${place.url}" target="_blank">
                <li>
                    <span class="name">${nameName} <br> ${(place.price)} <br><br><sup>${shop} ${shop1} ${shop2} ${shop3} ${shop4}</sup></span>
                    <img src="${place.imgurl}" alt="Drink Image" height="87.5" width="100">

                </li>
            </a>

        `;
      }).join('') || '<li> No Searches Found </li>';
      suggestions.innerHTML = html; 
    };

And my find matches function is我的查找匹配 function 是

function findMatches(wordToMatch, searchUrl, name) {
    const regEx = new RegExp(wordToMatch, 'gi');
    return name.filter(place => {
        return regEx.test(place.name) && place.url.startsWith(searchUrl);
    });
}

Besides the comment answer ( of just using const matchArray = findMatches(searchText, searchUrl, name).slice(0,1); ) which is a quick hack, a more valid way would be to directly search just for the first instead of finding all and then using only the first.除了快速破解的评论答案(仅使用const matchArray = findMatches(searchText, searchUrl, name).slice(0,1); )之外,更有效的方法是直接搜索第一个而不是查找all 然后只使用第一个。

 function displayMatches() { const searchText = '20 pack'; const searchUrl = document.querySelector('.custom-select').value; const place = findSingleMatches(searchText, searchUrl, name); if (place) { const regex = new RegExp(searchText); const nameName = place.name.replace(regex, `<span class="hl">${searchText}</span>`); var shop = place.url.replace(/(centra)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase()); var shop1 = place.url.replace(/(tesco)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase()); var shop2 = place.url.replace(/(aldi)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase()); var shop3 = place.url.replace(/(lidl)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase()); var shop4 = place.url.replace(/(supervalu)|[^]/g, '$1').replace(/^\w/, c => c.toUpperCase()); html = ` <li> <a href="${place.url}" target="_blank"> <span class="name">${nameName} <br> ${(place.price)} <br><br><sup>${shop} ${shop1} ${shop2} ${shop3} ${shop4}</sup></span> <img src="${place.imgurl}" alt="Drink Image" height="87.5" width="100"> </a> </li> `; } else { html = '<li> No Searches Found </li>' } suggestions.innerHTML = html; }; function findSingleMatch(wordToMatch, searchUrl, name) { return name.find(place => { const regEx = new RegExp(wordToMatch, 'gi'); return regEx.test(place.name) && place.url.startsWith(searchUrl); }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM