开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜