Quickly Check if Array Elements Satisfy Given Criteria in Javascript
Use Array.some() and Array.every() to check whether an array has elements that satisfy the given criteria. Consider the below array - const nums = [3, 5, 6, 1, 2]; You can check whether the elements satisfy a specified criteria with below code - console.log(nums.some((val, index) => val > 3)); // true console.log(nums.some((val, index) => val > 10)); // false You can do the same for objects in the array. const fruits = [(apple = { color: "red" }), (orange = { color: "orange" })]; console.log(fruits.some((val, index) => val.color == "red")); // true console.log(fruits.some((val, index) => val.color == "blue")); // false In a similar vein, you can check whether every element in the array satisfies a given criteria. ...