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 f开发者_运维问答or 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.
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>'));
});
精彩评论