In this Article we will go through how to check if an array contains a value matching some criterias only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function
.
Let's define this short function:
const contains = (arr, criteria) => arr.some(v => criteria(v));
const contains = (arr, criteria) => arr.some(criteria);
const contains = (arr,criteria) => arr.filter(criteria).length > 0;
contains([10, 20, 30], v => v > 25 ); // true
contains([10, 20, 30], v => v > 100 || v < 15 ); // true
contains([10, 20, 30], v => v > 100 ); // false