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.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
pmark019
  • 1,199
  • 5
  • 15
  • 24
  • http://stackoverflow.com/questions/784012/javascript-equivalent-of-phps-in-array Does this help? – Ariane Jul 12 '13 at 02: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 02:53
  • 1
    [Array.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Matt Burland Jul 12 '13 at 02:54

5 Answers5

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;
});
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
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;
}
Casey Flynn
  • 13,654
  • 23
  • 103
  • 194
  • +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 03:04
  • just noticed the === is it the same with == – pmark019 Jul 12 '13 at 03: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: http://www.impressivewebs.com/why-use-triple-equals-javascipt/ – Casey Flynn Jul 12 '13 at 04:33
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

tike
  • 2,234
  • 17
  • 19
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); 
Jaydeep
  • 1,686
  • 1
  • 16
  • 29
shigatsu
  • 48
  • 8
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

Data Crusader
  • 425
  • 1
  • 4
  • 14