52
_.uniq(array)
_uniq創建一個數組的副本,使用SameValueZero規則來比較,只有第一次出現的每個元素被保留,也就是去重。結果數組元素的順序取決於它們在數組中出現的位置
參數
[arrays] (...Array): 需要去重的數組
返回值
(Array): 返回去重后的新數組
例子
_.uniq([2, 1, 2]); // => [2, 1]
源代碼:
import baseUniq from './.internal/baseUniq.js' /** * Creates a duplicate-free version of an array, using * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) * for equality comparisons, in which only the first occurrence of each element * is kept. The order of result values is determined by the order they occur * in the array. * * @since 0.1.0 * @category Array * @param {Array} array The array to inspect. * @returns {Array} Returns the new duplicate free array. * @see uniqBy, uniqWith * @example * * uniq([2, 1, 2]) * // => [2, 1] */ //創建一個數組的副本,使用SameValueZero規則來比較,只有第一次出現的每個元素被保留,也就是去重。 //結果數組元素的順序取決於它們在數組中出現的位置 function uniq(array) { return (array != null && array.length) ? baseUniq(array) : [] //如果數組不為空並且數組有長度,調用baseUniq處理,否則返回空數組 } export default uniq
baseUniq
import SetCache from './SetCache.js' import arrayIncludes from './arrayIncludes.js'//判斷數組是否包含給定值 import arrayIncludesWith from './arrayIncludesWith.js'//判斷數組是否包含給定值,區別是它的comparator需要作為參數傳入 import cacheHas from './cacheHas.js' import createSet from './createSet.js' import setToArray from './setToArray.js' /** Used as the size to enable large array optimizations. */ const LARGE_ARRAY_SIZE = 200 /** * The base implementation of `uniqBy`. * * @private * @param {Array} array The array to inspect. * @param {Function} [iteratee] The iteratee invoked per element. * @param {Function} [comparator] The comparator invoked per element. * @returns {Array} Returns the new duplicate free array. */ //uniqBy的基礎實現,數組去重 function baseUniq(array, iteratee, comparator) { let index = -1//循環索引 let includes = arrayIncludes//類似數組的includes方法,判斷數組是否包含給定值 let isCommon = true//有沒有自定義比較器或者有沒有開啟緩存的標記 const { length } = array//數組長度 const result = []//結果數組 let seen = result//臨時數組,用來存入不重復的數組元素值 if (comparator) {//如果傳遞了自定義比較器,incledes方法就用arrayIncludesWith isCommon = false includes = arrayIncludesWith } else if (length >= LARGE_ARRAY_SIZE) {//如果數組長度超過200,就設置緩存 const set = iteratee ? null : createSet(array)//如果沒有iteratee和comparator,就直接使用ES6的set類型去重 if (set) {//如果可以用set類型去重,直接新建set對象然后再轉換成數組后返回結果 return setToArray(set) } isCommon = false//否則開啟數組緩存 includes = cacheHas seen = new SetCache } else {//如果沒有自定義比較器,數組長度也沒有超過200 seen = iteratee ? [] : result } outer: while (++index < length) {//遍歷數組 let value = array[index]//當前元素 const computed = iteratee ? iteratee(value) : value//iteratee處理后的當前元素 value = (comparator || value !== 0) ? value : 0//如果沒有傳遞comparator,將-0變為0 if (isCommon && computed === computed) {//如果沒有傳遞自定義比較器,或者沒有開啟緩存,並且computed不是NaN let seenIndex = seen.length//seen數組的長度 while (seenIndex--) {//遍歷seen數組,如果有和當前值一樣的,就跳出本次循環直接進行下一次 if (seen[seenIndex] === computed) { continue outer } } if (iteratee) {//如果有iteratee參數,seen結尾插入當前不重復的元素 seen.push(computed) } result.push(value)//結果數組結尾插入當前不重復的元素 } else if (!includes(seen, computed, comparator)) {//如果有自定義比較器或者開啟了緩存,並且seen中沒有當前元素 if (seen !== result) {//開啟緩存的情況seen數組要插入新值 seen.push(computed) } result.push(value)//結果數組插入新值 } } return result } export default baseUniq