0

Assuming that I have an array that looks like the one below:

["file1.java", "file32.js", "file44.java", "file4.html", "file15.js"]

I would like to filter out only the elements that end in ".java". Therefore I would like to somehow be able to search for the substring ".java" as I'm iterating through each element in my array. How do I go about doing this? Thank you

1
  • If you need to filter out elements that end in java, you'd have to use a regex as you iterate. Commented Oct 31, 2013 at 22:00

1 Answer 1

1

You can use the filter() method of the Array type:

var files = ["file1.java", "file32.js", "file44.java", "file4.html", "file15.js"];
var filtered = files.filter(function(item) {
        return item.endsWith(".java");
    }
);
console.log(filtered);

Not the answer you're looking for? Browse other questions tagged or ask your own question.