_.some(collection, [predicate=_.identity])
91
_.some(collection, [predicate=_.identity])
_.some检查predicate为任意数组元素调用后是否返回true,一旦predicate返回true,遍历就停止
参数
collection (Array|Object): 需要遍历的集合
[predicate=_.identity] (Function): 每次遍历调用的方法
返回值
(boolean): 任意元素只要通过predicate的检查,就返回true,否则false
例子
_.some([null, 0, 'yes', false], Boolean); // => true var users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false } ]; // The `_.matches` iteratee shorthand. _.some(users, { 'user': 'barney', 'active': false }); // => false // The `_.matchesProperty` iteratee shorthand. _.some(users, ['active', false]); // => true // The `_.property` iteratee shorthand. _.some(users, 'active'); // => true
源代码:
省略iteratee的处理,想看iteratee的处理看这里,https://www.cnblogs.com/hahazexia/p/9901435.html
/** * Checks if `predicate` returns truthy for **any** element of `array`. * Iteration is stopped once `predicate` returns truthy. The predicate is * invoked with three arguments: (value, index, array). * * @since 5.0.0 * @category Array * @param {Array} array The array to iterate over. * @param {Function} predicate The function invoked per iteration. * @returns {boolean} Returns `true` if any element passes the predicate check, * else `false`. * @example * * some([null, 0, 'yes', false], Boolean) * // => true */ //检查predicate为任意数组元素调用后是否返回true,一旦predicate返回true,遍历就停止 function some(array, predicate) { let index = -1//循环索引 const length = array == null ? 0 : array.length//数组长度 while (++index < length) {//遍历数组 if (predicate(array[index], index, array)) {//一旦predicate返回true,就返回true return true } } return false//否则返回false } export default some