2

I have searched across the web though have not had any luck in correcting my issue. What I want to do is search an array for a substring and return the result. An example of the array is like this:

the_array = ["PP: com.package.id, NN: Package Name","PP: com.another.id, NN: Another Name"];

What I want to do is search the_array for com.package.id making sure that it appears between the "PP:" and ",". Also please note that the array will contain several thousand values. Hope you can help, thank you.

2

1 Answer 1

2

Easy way:

the_array.join("|").indexOf([str]) >= 0;

Other ways would be to loop thru the array using .each() or a simple for loop

Array.prototype.each = function(callback){
    for (var i =  0; i < this.length; i++){
        callback(this[i]);
    }
}

the_array.each(function(elem){
    console.log(elem.indexOf('<searchString goes here>'));
});
5
  • each is not a JavaScript function. Commented Jul 30, 2011 at 22:37
  • 1
    FWIW, ECMAScript 5 introduced forEach: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… You can also find an alternative implementation there. But it seems to me that each is irrelevant for the problem. Commented Jul 30, 2011 at 22:46
  • Yes and Yes. Since ES5 is not universally supported yet, we need these workarounds.
    – Mrchief
    Commented Jul 30, 2011 at 22:48
  • @Mrchief it is 'each.indexOf'. Please change it. Commented Jul 23, 2013 at 12:12
  • @KamalReddy: It's pseudocode, note that [str] is not an array but placeholder for search string in question. But I'll edit for clarity anyway.
    – Mrchief
    Commented Jul 24, 2013 at 3:07

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