skip to Main Content
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",

3

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. You don’t even need javascript!

    <label for="ice-cream-choice">Search Items</label>
    <input list="my-items-list">
    
    <datalist id="my-items-list">
      <option value="Degree Foundation">
      <option value="Business HND With Marketing Management">
      <option value="Business HND With Human Resource Management">
      <option value="Business HND With Business Analytics">
      <option value="Australian Degree Year 1 Diploma">
      <option value="BA (Hons) International Business and Finance">
      <option value="BA Global Business (Top-Up)">
      <option value="BSc (Hons) Business Management with International Business">
      <option value="BSc (Hons) Business Management with Marketing Management">
      <option value="BSc (Hons) Business Management with Human Resource Management">
      <option value="MBA International Business">
      <option value="MBA (General)">
      <option value="MBA in Human Resources Management">
      <option value="MBA with Business Analytics">
      <option value="MSC Strategic Marketing">
      <option value="CIMA">
    </datalist>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search