Lodash源码分析-uniq,uniqBy,uniqWith

lodash源码研读之uniq,uniqBy,uniqWith

一、源码地址

  •  GitHub 地址: GitHub - lodash/lodash: A modern JavaScript utility library delivering modularity, performance, & extras.
  • 官方文档地址: Lodash 官方文档

二、结构分析

uniq,uniqBy,uniqWith基于baseUniq模块。

三、函数介绍

下面依次介绍各个模块。

1.baseUniq模块

  function baseUniq(array, iteratee, comparator) {
            var index = -1,
                includes = arrayIncludes,
                length = array.length,
                isCommon = true,
                result = [],
                seen = result;

            if (comparator) {
                isCommon = false;
                includes = arrayIncludesWith;
            } else if (length >= LARGE_ARRAY_SIZE) {
                var set = iteratee ? null : createSet(array);
                if (set) {
                    return setToArray(set);
                }
                isCommon = false;
                includes = cacheHas;
                seen = new SetCache;
            } else {
                seen = iteratee ? [] : result;
            }
            outer:
                while (++index < length) {
                    var value = array[index],
                        computed = iteratee ? iteratee(value) : value;

                    value = (comparator || value !== 0) ? value : 0;
                    if (isCommon && computed === computed) {
                        var seenIndex = seen.length;
                        while (seenIndex--) {
                            if (seen[seenIndex] === computed) {
                                continue outer;
                            }
                        }
                        if (iteratee) {
                            seen.push(computed);
                        }
                        result.push(value);
                    } else if (!includes(seen, computed, comparator)) {
                        if (seen !== result) {
                            seen.push(computed);
                        }
                        result.push(value);
                    }
                }
            return result;
        }

baseUniq 函数用于在数组中去除重复的元素,并返回一个包含唯一元素的新数组。这个函数的主要作用是去重,并且提供了一些额外的选项,如 iteratee 函数用于对数组元素进行转换,以及 comparator 函数用于自定义比较逻辑。

  • index = -1:用于遍历数组的索引。
  • includes = arrayIncludes:用于检查元素是否在数组中。

  • length = array.length:数组的长度。

  • isCommon = true:标记是否使用普通的方式进行去重。

  • result = []:存储去重后的结果数组。

  • seen = result:用于存储已经见过的元素。

2.uniq函数

 function uniq(array) {
            return (array && array.length) ? baseUniq(array) : [];
        }

uniq 函数是一个简单的包装函数,用于简化 baseUniq 函数的调用。它主要检查输入的数组是否存在并且具有长度,如果是,则调用 baseUniq 函数进行去重操作,否则返回一个空数组。

3.uniqBy函数

 function uniqBy(array, iteratee) {
            return (array && array.length) ? baseUniq(array, getIteratee(iteratee, 2)) : [];
        }

 uniqBy 函数是一个用于通过指定迭代器(iteratee)对数组进行去重的函数。它首先检查输入的数组是否有效(存在且长度大于 0),如果有效,则调用 baseUniq 函数进行去重操作,否则返回一个空数组。

4.uniqWith函数

 function uniqWith(array, comparator) {
            comparator = typeof comparator == 'function' ? comparator : undefined;
            return (array && array.length) ? baseUniq(array, undefined, comparator) : [];
        }

uniqWith 函数是一个用于通过自定义比较器(comparator)对数组进行去重的函数。它首先检查输入的数组是否有效(存在且长度大于 0),然后确保 comparator 是一个函数,最后调用 baseUniq 函数进行去重操作。如果数组无效,则返回一个空数组。

四、总结

uniq函数对数组进行简单的去重操作,去除数组中重复的元素。

uniqBy函数通过指定的迭代器对数组进行去重,根据迭代器返回的值进行比较。

uniqWith函数通过指定的比较器对数组进行去重,根据比较器返回的比较结果进行比较。

你可能感兴趣的:(Loadsh源码分析,javascript,前端)