[英]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 然后只使用第一个。
findSingleMatch
that instead of filter
uses the find
method to find the first item in the array.您可以创建一个findSingleMatch
代替filter
使用find
方法来查找数组中的第一项。.test
which takes into account the previous index of a match when searching for the next ( see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test#Description ).在您的情况下,您也不应该重复使用相同的 RegExp object,因为您使用的.test
在搜索下一个匹配项时会考虑前一个索引(请参阅https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test#Description )。<a>
tag inside the li
as it is invalid to put <a>
directly under a <ul>
最后你应该把<a>
标签放在li
里面,因为把<a>
直接放在<ul>
下是无效的 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.