dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: searchfunction find if value is in array

  1. #1
    Join Date
    Apr 2017
    Posts
    14

    Question searchfunction find if value is in array

    Hello!

    I am building a search function for my website. When the user inputs certain word he shall be directed to a corresponding site.
    This works so far.

    Now I want to give the user more than one string-opition to find the site.
    My idea was now to create one array for each site, with the terms that correlate with that site, here this array is "kokos".
    Now the search value must be compared to the values in the array to find out if it is one of them.

    I first used an if-statement with ORs, it works, but is way too much to write.
    Then I tried the (item in list) expression. It works in the for loop, but not in the if statement. I tried already to put the whole for loop into the if-statement or the result from the for-loop, but I don't really get out what I want.

    Code:
    var suche = document.getElementById("search_term").value;
        suche = String(suche);
        var kokos = ["Kokos", "Kokospalme", "kokospalme"];
        
       /* for (suche in kokos) {  // works
            window.location.href =  ps + "p_kokospalme.html";
        } */
        
        if (suche in kokos) { // does not work
            window.location.href = ps + "p_kokospalme.html"; // Kokosnusspalme
        } else if (suche == "Dattelpalme" || suche == "dattelpalme") {
            window.location.href = ps + "p_dattelpalme.html"; // Dattelpalme
        } else if (suche == "königspalme" || suche == "Königspalme") {
            window.location.href = ps + "p_koenigspalme.html"; //Koenigspalme
        } else if (suche == "Hanfpalme" || suche == "hanfpalme") {
            window.location.href = ps + "p_hanfpalme.html"; // Hanfpalme
    ...

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    542
    You can put all the search terms in a string and use strstr() on what the user entered. If whatever the user entered in was that string, the function will return something higher than -1.

    You might want to create a 2-d array to hold new website destinations and search terms so it won't be necessary to put together a long chain of "if else" statements. A loop could process all of them and it would be easy to add another site and search terms without changing the code.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,723
    result = your_array.indexOf( search, offset)

    for example...
    Code:
    var a = ["a",1,"better",2,"way",3,"to",4,"find",5,"something"];
    var b = a.indexOf("to");
    console.log("> to is " + (b>-1 ? "" : "not") + "in the array" );
    b = a.indexOf("chance");
    console.log("> chance is " + (b>-1 ? "" : "not ") + "in the array");
    will output in the console the following...
    > to is in the array
    > chance is not in the array

    strstr() is a PHP function not a JavaScript function, so unless you do this server-side, your strstr() won't help much.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Jun 2009
    Location
    Jacksonville, FL U.S.A.
    Posts
    30
    Code:
    var input = document.getElementById("search_term").value.toLowerCase(); // Already a string.
    var ps = ""; // Dunno what this is, it isn't defined in the original.
    var terms = {
      kokos: "p_kokospalme.html",
      kokospalme: "p_kokospalme.html",
      dattelpalme: "p_dattelpalme.html",
      königspalme: "p_koenigspalme.html",
      hanfpalme: "p_hanfpalme.html"
    };
    var match = terms[input];
    
    if (match !== undefined) {
      window.location.href = ps + match;
    }
    Maybe something like that would work.
    Last edited by Clock Out; 05-17-2017 at 09:36 PM.

  5. #5
    Join Date
    Apr 2017
    Posts
    14
    Thank you very much for your answers!
    With this I was able to solve the issue.
    I used the indexOf-method and it works good for me.


    You might want to create a 2-d array to hold new website destinations and search terms so it won't be necessary to put together a long chain of "if else" statements. A loop could process all of them and it would be easy to add another site and search terms without changing the code.
    I thought about something like this, but could not get my mind around how to do it.
    I would basically need a function that loops through the arrays in an array and then return the name of the array if it finds the searchterm there.
    But then it would need to be an object, to give it named entrys, then it can return the name of the object, where the fitting searchterms are inside, and with the name I could get the proper URL to go to. But I am not sure if this is less code to write in the end.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles