Two new logical operations on array elements in ECMA Script 5 some, every
Array.some (function (item) {});
Array.every (function (item) {});
The Every method means that when each element of the array returns true in the decision function, the final result is true, which is equivalent to the logical operator &&
Some when one of them returns true, the final result is true, equivalent to the logical operator | |.
Take the following example to illustrate:
var items = [1, 2, 3, 4, 5]; var res = items.every (function (item) { return item > 3; }); Console.log (RES); False items = [' A ', 2, 3, 4, 5]; res =www.90168.org items.every (function (item) { return!isnan (item);//Check whether it is a value }); Check whether each element is a digital console.log (res);//False items = [' A ', 2, 3, 4, 5]; res = items.some (function (item) { return!isnan (item);//Check whether it is a value }); Console.log (RES); True
Logical OR, of course, the logic and operation may encounter the case of an empty array, so according to the mathematical Convention, when the array is empty, some directly returns false, every directly returns True,
So when designing to a feature business, you need to be aware of this, and it's best to add a qualification to ensure that the array is not empty and that the code is intended to be read more clearly.
At the same time, it is understood from the specification that, as with logic and similarity, when one of the judgments returns false, the entire calculation stops directly, returning the final result false to improve efficiency. So please do not add other business logic or execute code in the decision function, if required, use map instead.
Logic judgment of every and some methods in JS ECMAScript 5