Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Searching for a string inside of an array

    Hi,

    I am trying to search an array for a string and then if it is found alert it.

    I have found examples of how to iterate the array and how to use .IndexOf to return a true false statement as to whether the array includes the string, but i don't know what to do after that and how to display the string if its found.

    Any help appreciated..

    Thanks

  • #2
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Hi kev3kev3

    Well it really depends on a couple of things. Do you want to search for EVERY instance of the string in the array? (for example if the string appears more than once in the array do you want to find every occurance or just the first?)

    Also, what exactly do you want to alert? The string you are searching for? or the array entry where it was found?

    Regardless, this function should hopefully give you some idea:

    Code:
    var myArray = ["hello", "test", "foo", "bar"];
    
    function searchArray(searchString)
    {
    	for(var i=0;i<myArray.length;i++)
    	{
    		if(myArray[i].indexOf(searchString)!=-1)
    		{
    			alert("'"  + searchString + "' is present in '" + myArray[i] + "'");
    		}
    	}
    }
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • Users who have thanked thesam101 for this post:

    kev3kev3 (12-14-2011)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Code:
    var example = [
        "T'was brillig and the slithy toves",
        "Did gyre and gymbal in the wabe.",
        "All mimsy were the borogoves,",
        "And the mome rath outgrabe."
    ];
    
    var find = "mimsy";
    var found = null;
    
    for ( var i = 0; i < example.length; ++i )
    {
        if ( example[i].indexOf(find) >= 0 )
        {
            found = example[i].replace(find,"<b>" + find + "</b>");
            break; // don't look further
        }
    }
    if ( found != null ) document.write(found);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    kev3kev3 (12-14-2011)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Well, thesam is faster, but my answer is more fun. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you both that really helps

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    I'm late to the dance, but if you need an alternative that mixes both solutions:
    Code:
    <script type="text/javascript">
    //<![CDATA[
    var example = [
        "T'was brillig and the slithy toves",
        "Did gyre and gymbal in the wabe.",
        "All mimsy were the borogoves,",
        "And the mome rath outgrabe."
    ];
    
    var str = example.join('|');
    
    var find = "mimsy";
    var found = str.indexOf(find);
    if (found != -1) { alert('Found: '+find); }
                else { alert(find+' was not found'); }
    
        find = "mixmsy";
        found = str.indexOf(find);
    if (found != -1) { alert('Found: '+find); }
                else { alert(find+' was not found'); }
    
    //]]>
    </script>
    Could easily be placed into a function that returned true/false.

  • #7
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can anyone possibly tell me how i would ouput all objects within an array,that the parameter of the function takes within the array.
    I am currently using a for in loop then an if for this parameter if it matches the object in the array.But it only shows the object with the higher index.I want to display all objects which match this parameter.

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Exclamation

    Quote Originally Posted by Surely View Post
    Can anyone possibly tell me how i would ouput all objects within an array,that the parameter of the function takes within the array.
    I am currently using a for in loop then an if for this parameter if it matches the object in the array.But it only shows the object with the higher index.I want to display all objects which match this parameter.
    Your request has nothing to do with the originator's question of this thread.

    You would be better served if you start your own thread
    rather than hijack this one.

    Plus, you should at least attempt to show some code for your problem.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    use [].filter() to gather certain objects.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

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