How can I return only the objects in an array that meet a certain criteria using javascript?
For instance, if I have ['apple','avocado','banana','cherry'] and want to only output fruit that begin with the letter 'A'.
EDIT:
Took Sean Kinsey's function below and tried to make it more flexible by passing in the array and letter to match:
function filterABC(arr,abc) {
var arr = arr;
var filtered = (function(){
var filtered = [], i = arr.length;
while (i--) {
if ('/^' + abc + '/'.test(arr[i])) {
filtered.push(arr[i]);
}
}
return filtered;
})();
return filtered.join();
}
Trying to call it with filterABC(arr,'A') or filterABC(arr,'A|B|C|') to output all matches from A to C but having trouble with this part.