JavaScript - get array element fulfilling a condition
Use ES6 Array.filter()
and arrow functions with expression body:
myArray.filter(x => x > 5)
A bit more concise than @Beauty's answer.
In most browsers (not IE <= 8) arrays have a filter
method, which doesn't do quite what you want but does create you an array of elements of the original array that satisfy a certain condition:
function isGreaterThanFive(x) {
return x > 5;
}
[1, 10, 4, 6].filter(isGreaterThanFive); // Returns [10, 6]
Mozilla Developer Network has a lot of good JavaScript resources.