2

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.

3
  • stackoverflow.com/questions/784012/… Does this help? Commented Jul 12, 2013 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? Commented Jul 12, 2013 at 2:53
  • 1
    Array.filter Commented Jul 12, 2013 at 2:54

5 Answers 5

3

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;
});
Sign up to request clarification or add additional context in comments.

1 Comment

If IE < 9 need to be supported then may have to use a shim
2

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); 

1 Comment

simplest and easiest of all the answers. Thanks!
2

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;
}

3 Comments

+1 for caching array.length and type checking, you should also use === -1 when checking the return value of indexOf
just noticed the === is it the same with ==
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
2

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

Comments

0

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

2 Comments

will this show foofy and foofa or only foo?...sorry I don't have a computer right now so I can't try it...
It will return all array string elements that contains the word foo in it. In this case: foofy, foofa and foo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.