简体   繁体   English

如何在 JavaScript 中实现“包含”搜索

[英]How to implement a 'contains' search in JavaScript

I'm creating a search box that allows you to search for different companies.我正在创建一个搜索框,允许您搜索不同的公司。 I'd like the search box to perform a 'contains' search.我希望搜索框执行“包含”搜索。 For example, let's say that I want to look up the company ExxonMobil Oil Corp. Typing in any of the following should include the company in the list of results (this isn't exhaustive):例如,假设我想查找 ExxonMobil Oil Corp 公司。键入以下任何内容应将公司包含在结果列表中(这并不详尽):

  • oil
  • corp公司
  • oil corp石油公司
  • exxonmobil埃克森美孚
  • exxonmobil oil埃克森美孚石油
  • exxonmobil oil corp埃克森美孚石油公司

The words don't have to be complete, just to be clear.单词不必完整,只要清晰即可。 The phrase 'oil co', for instance, should still bring up a result.例如,短语“oil co”仍应显示结果。

Typing in 'exxonmobil corp', however, will not bring up the company as a result since 'corp' does not immediately follow 'exxonmobil' in the company's name.但是,输入“exxonmobil corp”不会显示公司,因为“corp”不会直接跟在公司名称中的“exxonmobil”之后。

Is there a go-to method for implementing this type of search, keeping time efficiency in mind?是否有实现此类搜索的首选方法,同时牢记时间效率? In my case, there can be thousands of companies to search through.就我而言,可以搜索数千家公司。 And I'd like to be able to display the list of results on the fly as the user is typing into the search box.我希望能够在用户输入搜索框时即时显示结果列表。

I'm aware of the trie data structure, but from what I've read, it seems to work best for 'starts with' searches.我知道 trie 数据结构,但从我读过的内容来看,它似乎最适合“开头为”搜索。 So it wouldn't match searches like 'oil corp', 'oil', or 'corp' with ExxonMobil Oil Corp. Perhaps there's a way to tweak the trie to do as I want, but I'm just not sure if that's the best way to go.所以它不会匹配埃克森美孚石油公司的“oil corp”、“oil”或“corp”之类的搜索。也许有一种方法可以调整 trie 以按照我的意愿进行,但我不确定这是否是go 的最佳方式。

Thank you for the responses.感谢您的回复。 A few of you suggested looking into String.prototype.includes() .你们中的一些人建议查看String.prototype.includes() I gave that a try, and it does seem to work well with no performance issues.我试了一下,它似乎运行良好,没有性能问题。

100 companies is fast. 100家公司很快。

 const companies = [ "Arcade Flower Shop", "Madam Malkin's Robes for All Occasions", "Victoria's Circuit", "33¢ Store", "El Banco Corrupto", "Silver Shamrock", "Stay Puft Corporation", "Wonka Industries", "Blue Moon Detective Agency", "The Foundation", "Macmillan Toys", "The Reef", "Merrick BioTech", "The Peach Pit", "The Korova Milkbar", "Paper Street Soap Company", "Mel's Diner", "Dunder Miflin", "The Everything Store", "Rodbell's", "Rex Kwan Do", "The Fairly Oddparents", "Vitameatavegamin", "Bushwood Country Club", "Consumer Recreation Services", "The Rusty Anchor", "IPS (International Parcel Services)", "Pendant Publishing", "Lacuna Inc.", "HAL Labs", "Life Extension", "Rekall", "Bluehound Bus Line", "Atlantic American Airlines", "KACL", "Flingers", "Burrito Explosion", "Fatso's", "The Max", "McDowell's", "Bada Bing", "Wu-Tang Financial", "Wally World", "The Dharma Initiative", "The Leftorium", "Edna's Edibles", "Daily Pl.net", "21 Jump Street", "The Enterprise", "Powell Family", "Central Perk", "Night Court", "Arnold's Drive-In", "WKRP", "Moe's Tavern", "Lomax Industries", "Hudsucker Industries", "Los Pollos Hermanos", "Chubby's", "Mugatu Industries", "The Daily Bugle", "Globex Corporation", "Entertainment 720", "Soylent Corporation", "SS Minnow", "TGS with Tracy Jordan", "Grace Adler Designs", "Pierce & Pierce", "Wayne Enterprises", "Cheers", "Goliath National Bank", "Pricemart", "Career Transitions Corporation", "Bluth's Original Frozen Banana", "Livingston", "Luke's Diner", "Adventureland", "Buy-N-Large", "Average Joe's Gym", "Duff Beer", "Michael Scott Paper Company", "Brawndo", "Fisher & Sons", "Mitch and Murray", "Multi National United", "Oscorp", "Pizza Pl.net", "Momcorp", "Ewing Oil", "Prestige Worldwide", "Tyrell Corporation", "Omni Consumer Products", "Monsters Inc.", "Ghostbusters", "Pied Piper", "TelAmeriCorp", "Yakonomo Corporation", "Mega Lo Mart", "Vandelay Industries", "Frosty Palace", "Sterling Cooper Draper Pryce", "MIB", "The Smash Club" ]; const search = document.getElementById("search"); const output = document.getElementById("output"); const filter = (evt) => { const val = evt.target.value; if (val.length < 1) return output.value = ""; output.value = companies.filter(company => company.toLowerCase().includes(val.toLowerCase())).join("\n"); } search.addEventListener("keyup", filter);
 input, textarea { margin-top: 1em; }
 <link href="https://unpkg.com/marx-css/css/marx.min.css" rel="stylesheet" /> <main> <input type="text" id="search" /> <textarea rows=4 id="output"></textarea> </main>

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

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