_.max(array)
Array max _.
2023-09-11 14:15:01 时间
177
_.max(array)
_.max计算出一个数组中最大的值。如果数组是空数组或者假值,就返回undefined
参数
array (Array): 需要遍历找最大值的数组
返回值
(*): 返回最大值
例子
_.max([4, 2, 8, 6]); // => 8 _.max([]); // => undefined
源代码
/** * lodash (Custom Build) <https://lodash.com/> * Build: `lodash modularize exports="npm" -o ./` * Copyright jQuery Foundation and other contributors <https://jquery.org/> * Released under MIT license <https://lodash.com/license> * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors */ /** `Object#toString` result references. */ var symbolTag = '[object Symbol]'; /** Used for built-in method references. */ var objectProto = Object.prototype; /** * Used to resolve the * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) * of values. */ var objectToString = objectProto.toString; /** * The base implementation of methods like `_.max` and `_.min` which accepts a * `comparator` to determine the extremum value. * * @private * @param {Array} array The array to iterate over. * @param {Function} iteratee The iteratee invoked per iteration. * @param {Function} comparator The comparator used to compare values. * @returns {*} Returns the extremum value. */ //_.max和_.min的基础实现,接收一个比较器来比较出极值 function baseExtremum(array, iteratee, comparator) { var index = -1,//循环索引 length = array.length;//数组长度 while (++index < length) {//循环数组 var value = array[index],//数组当前循环值 current = iteratee(value);//当前循环值由iteratee处理后的结果 if (current != null && (computed === undefined ? (current === current && !isSymbol(current)) : comparator(current, computed) )) { //如果current不是null且computed还没有值,并且current不是NaN且不是Symbol对象,就创建computed和result存下当前current //否则就用comparator比较current和computed的大小 var computed = current, result = value; } } return result;//返回找到的极值 } /** * The base implementation of `_.gt` which doesn't coerce arguments. * * @private * @param {*} value The value to compare. * @param {*} other The other value to compare. * @returns {boolean} Returns `true` if `value` is greater than `other`, * else `false`. */ //_.gt基础实现,返回value是否大于other的布尔值 function baseGt(value, other) { return value > other; } /** * Checks if `value` is object-like. A value is object-like if it's not `null` * and has a `typeof` result of "object". * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is object-like, else `false`. * @example * * _.isObjectLike({}); * // => true * * _.isObjectLike([1, 2, 3]); * // => true * * _.isObjectLike(_.noop); * // => false * * _.isObjectLike(null); * // => false */ //判断一个值是否是object-like对象,不是null且typeof返回object function isObjectLike(value) { return !!value && typeof value == 'object'; } /** * Checks if `value` is classified as a `Symbol` primitive or object. * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`. * @example * * _.isSymbol(Symbol.iterator); * // => true * * _.isSymbol('abc'); * // => false */ //判断一个值是否是原生Symbol对象 function isSymbol(value) { return typeof value == 'symbol' || (isObjectLike(value) && objectToString.call(value) == symbolTag); } /** * This method returns the first argument it receives. * * @static * @since 0.1.0 * @memberOf _ * @category Util * @param {*} value Any value. * @returns {*} Returns `value`. * @example * * var object = { 'a': 1 }; * * console.log(_.identity(object) === object); * // => true */ //此方法返回接收到的第一个参数 function identity(value) { return value; } /** * Computes the maximum value of `array`. If `array` is empty or falsey, * `undefined` is returned. * * @static * @since 0.1.0 * @memberOf _ * @category Math * @param {Array} array The array to iterate over. * @returns {*} Returns the maximum value. * @example * * _.max([4, 2, 8, 6]); * // => 8 * * _.max([]); * // => undefined */ //计算出一个数组中最大的值。如果数组是空数组或者假值,就返回undefined function max(array) { return (array && array.length) ? baseExtremum(array, identity, baseGt) : undefined; //如果是空数组或者假值,返回undefined,否则调用baseExtremum计算最大值 } module.exports = max;
相关文章
- ES6 Array
- leetcode 之Remove Duplicates from Sorted Array(1)
- php 对多维数组排序array_multisort
- (LeetCode 153)Find Minimum in Rotated Sorted Array
- [Python] Accessing Array Elements
- [ES2016] Check if an array contains an item using Array.prototype.includes
- [Javascript] Create 2d array by using Array.from
- 【Codeforces 1108E1】Array and Segments (Easy version)
- 【Codeforces 1042D】Petya and Array
- JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token
- 关于拷贝常规数组、std::array和std::vector速度的一些测试
- Vue组件库新增的prop属性类型是Object或者Array时默认值的设置
- nginx学习七 高级数据结构之动态数组ngx_array_t
- Array对象的方法实现(5)----Array.prototype.includes(实现常规参数的功能)