ctrled
ctrled

Reputation: 23

Searching an array for a substring in Javascript

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.

Upvotes: 2

Views: 614

Answers (1)

Mrchief
Mrchief

Reputation: 76258

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

Upvotes: 2

Related Questions