...

View Full Version : Searching for a string inside of an array



kev3kev3
12-13-2011, 09:39 PM
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 :D

thesam101
12-13-2011, 10:19 PM
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? :D

Regardless, this function should hopefully give you some idea:



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] + "'");
}
}
}

:thumbsup:

Old Pedant
12-13-2011, 10:20 PM
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);

Old Pedant
12-13-2011, 10:21 PM
Well, thesam is faster, but my answer is more fun. <grin/>

kev3kev3
12-14-2011, 04:14 PM
Thank you both that really helps :D

jmrker
12-14-2011, 05:10 PM
I'm late to the dance, but if you need an alternative that mixes both solutions: :D


<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.

Surely
12-14-2011, 05:48 PM
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.

jmrker
12-14-2011, 11:05 PM
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.
:rolleyes:

rnd me
12-14-2011, 11:18 PM
use [].filter() to gather certain objects.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum