Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have an array that contains string.

var js_userBoxName = new Array();

I want to search the elements of the array if a string has the word "foo" it should displayed.

So for example my array contains the words {foofy, foofa, foo, awtsy}, foofy, foofa and foo will be displayed since they all contains the word foo.

share|improve this question
    
stackoverflow.com/questions/784012/… Does this help? – Ariane Jul 12 '13 at 2:51
2  
Is it the iteration through the array of the string dissection that you're having problems with? What have you tried? – DevlshOne Jul 12 '13 at 2:53
1  
Array.filter – Matt Burland Jul 12 '13 at 2:54
up vote 2 down vote accepted

You could also try this:

    var myarray = ["foofy", "foofa", "foo", "awtsy"];
    var searched_string = "foo"; 
    var foundmatch = [];

    for(i=0; i < myarray.length; i++){
        if(myarray[i].match(searched_string)){
            foundmatch.push(myarray[i]);
        }
    } 

    alert(foundmatch); 
share|improve this answer
    
simplest and easiest of all the answers. Thanks! – pmark019 Jul 12 '13 at 6:51

Check out the Array.filter method:

var arr = ['foofy', 'foofa', 'foo', 'awtsy'];
var fooItems = arr.filter(function (item) {
    return item.indexOf('foo') >= 0;
});

// Yields ['foofy', 'foofa', 'foo']

The linked MDN page contains a polyfill for browsers that do not support it. If you happen to be using jQuery, you could use $.grep:

var arr = ['foofy', 'foofa', 'foo', 'awtsy'];
var fooItems = $.grep(arr, function (item) {
    return item.indexOf('foo') >= 0;
});
share|improve this answer
    
If IE < 9 need to be supported then may have to use a shim – Arun P Johny Jul 12 '13 at 2:55

The following function should do the trick, it uses only standard acme script elements

function Find (myarray, searchterm){
    for (var i=0, len = myarray.length; i<len; i += 1){
         if (typeof(myarray[i]) === 'string' && myarray[i].search(searchterm) !== -1){
             // print or whatever
             //hint use a callback here that you pass in as an additional arugment
         }
    }
}

using search allows you to use regex if you need to check for something more complex

share|improve this answer

NOTE: Good performance tip is to always cache the length of your array when iterating over it rather than recalculating it for each loop execution. len = js_userBoxName.length

http://jsperf.com/caching-array-length/4

function () {
    for(var i = 0, len = js_userBoxName.length; i < len; i++){
      if(typeof js_userBoxName[i] === 'string'){
        if(js_userBoxName[i].indexOf('foo') == -1)
          return true;
      }
    }
    return false;
}
share|improve this answer
    
+1 for caching array.length and type checking, you should also use === -1 when checking the return value of indexOf – tike Jul 12 '13 at 3:04
    
just noticed the === is it the same with == – pmark019 Jul 12 '13 at 3:42
1  
It's called 'strict equals,' in the case of actual objects (not simple types like strings or numbers) it compares 'instance' equality (ie. is this object the exact same object as this object) and it's considered a best-practice by some. Check this out for a bit more info: impressivewebs.com/why-use-triple-equals-javascipt – Casey Flynn Jul 12 '13 at 4:33

The following should work:

var myArray = ['foofy', 'foofa', 'foo', 'awtsy'];

for ( var i = 0; i < myArray.length; i++ ) {
  if ( myArray[i].contains('foo') )
    console.log(myArray[i]);
}

prints: foofy foofa foo

Note: "awtsy" does not contain the pattern foo

share|improve this answer
    
Tip: stackoverflow.com/a/17499001/1850609 – acdcjunior Jul 12 '13 at 3:04
    
thanks! My change should look better! – Ninja Jul 12 '13 at 3:24
    
Now it looks good! +1! – acdcjunior Jul 12 '13 at 3:25
    
will this show foofy and foofa or only foo?...sorry I don't have a computer right now so I can't try it... – pmark019 Jul 12 '13 at 3:42
    
It will return all array string elements that contains the word foo in it. In this case: foofy, foofa and foo – Ninja Jul 12 '13 at 16:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.