如何基于传递的关键词使用纯JS获取建议结果。

huangapple go评论74阅读模式
英文:

how to get suggestions result based on passed keywords using vanilla js

问题

let suggestions = [
        "学位基础课程",
        "市场营销管理业务HND",
        "人力资源管理业务HND",
        "业务分析业务HND",
        "澳大利亚学位一年文凭",
        "国际商业与金融荣誉学士",
        "全球商业荣誉学士(补充课程)",
        "国际商业管理与国际业务荣誉学士",
        "市场营销管理与企业管理荣誉学士",
        "人力资源管理与企业管理荣誉学士",
        "国际商业MBA",
        "通用MBA",
        "人力资源管理MBA",
        "业务分析MBA",
        "战略营销MSc",
        "特许管理会计师(CIMA)",
    ];

inputs: 任何英文字符或字符,例如 - "hn", "mang"
期望输出: 包含上述数组中输入字符数组值的部分,例如 -

对于 - "hn"

    "市场营销管理业务HND",
    "人力资源管理业务HND",
    "业务分析业务HND",

对于 - "manag"

    "市场营销管理业务HND",
    "人力资源管理业务HND",
    "国际商业管理与市场营销管理荣誉学士",
    "国际商业管理与人力资源管理荣誉学士",
    "人力资源管理MBA",
英文:
let suggestions = [
        "Degree Foundation",
        "Business HND With Marketing Management",
        "Business HND With Human Resource Management",
        "Business HND With Business Analytics",
        "Australian Degree Year 1 Diploma",
        "BA (Hons) International Business and Finance",
        "BA Global Business (Top-Up)",
        "BSc (Hons) Business Management with International Business",
        "BSc (Hons) Business Management with Marketing Management",
        "BSc (Hons) Business Management with Human Resource Management",
        "MBA International Business",
        "MBA (General)",
        "MBA in Human Resources Management",
        "MBA with Business Analytics",
        "MSC Strategic Marketing",
        "CIMA",
    ];

inputs: any English character or characters for example - "hn", "mang"
expect output: which contains input character array values in the above array for example -

for - "hn"

    "Business HND With Marketing Management",
    "Business HND With Human Resource Management",
    "Business HND With Business Analytics",

for - "manag"

    "Business HND With Marketing Management",
    "Business HND With Human Resource Management",
    "BSc (Hons) Business Management with Marketing Management",
    "BSc (Hons) Business Management with Human Resource Management",
    "MBA in Human Resources Management",

答案1

得分: 2

你可以使用filter()方法与includes()方法来查找包含指定字符串的建议数组中的每个元素。

function findSuggestionsContaining(searchTerm) {
    return suggestions.filter(item => item.includes(searchTerm));
}

编辑我不确定这种方法的可扩展性如何但对于较小的建议列表它应该按预期工作无需使用外部库等
英文:

You can use the filter() method along with the includes() method to find every element in the suggestions array that includes a specified string.

function findSuggestionsContaining(searchTerm) {
    return suggestions.filter(item => item.includes(searchTerm));
}

Edit: I am not sure how scalable this approach is, but for "smaller" suggestion lists it should definitly work as expected without having to use external libaries etc.

答案2

得分: 1


英文:

You don't even need javascript!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;label for=&quot;ice-cream-choice&quot;&gt;Search Items&lt;/label&gt;
&lt;input list=&quot;my-items-list&quot;&gt;

&lt;datalist id=&quot;my-items-list&quot;&gt;
  &lt;option value=&quot;Degree Foundation&quot;&gt;
  &lt;option value=&quot;Business HND With Marketing Management&quot;&gt;
  &lt;option value=&quot;Business HND With Human Resource Management&quot;&gt;
  &lt;option value=&quot;Business HND With Business Analytics&quot;&gt;
  &lt;option value=&quot;Australian Degree Year 1 Diploma&quot;&gt;
  &lt;option value=&quot;BA (Hons) International Business and Finance&quot;&gt;
  &lt;option value=&quot;BA Global Business (Top-Up)&quot;&gt;
  &lt;option value=&quot;BSc (Hons) Business Management with International Business&quot;&gt;
  &lt;option value=&quot;BSc (Hons) Business Management with Marketing Management&quot;&gt;
  &lt;option value=&quot;BSc (Hons) Business Management with Human Resource Management&quot;&gt;
  &lt;option value=&quot;MBA International Business&quot;&gt;
  &lt;option value=&quot;MBA (General)&quot;&gt;
  &lt;option value=&quot;MBA in Human Resources Management&quot;&gt;
  &lt;option value=&quot;MBA with Business Analytics&quot;&gt;
  &lt;option value=&quot;MSC Strategic Marketing&quot;&gt;
  &lt;option value=&quot;CIMA&quot;&gt;
&lt;/datalist&gt;

<!-- end snippet -->

答案3

得分: 0

使用模糊搜索库,例如:https://github.com/leeoniya/uFuzzy

英文:

use a fuzzy search library like: https://github.com/leeoniya/uFuzzy

huangapple
  • 本文由 发表于 2023年7月31日 21:52:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定