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>'));
});
Sign up to request clarification or add additional context in comments.

5 Comments

each is not a JavaScript function.
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.
Yes and Yes. Since ES5 is not universally supported yet, we need these workarounds.
@Mrchief it is 'each.indexOf'. Please change it.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.