zoukankan      html  css  js  c++  java
  • underscore arrays

    1、_.first():返回array(数组)的第一个元素。传递 n参数将返回数组中从第一个元素开始的n个元素

    _.first = _.head = _.take = function(array, n, guard) {
      //n == null-->array[0];
      //n != null;guard == ture;-->array[0]
      //n != null;guard == false;-->返回前n个元素
    return (n != null) && !guard ? slice.call(array, 0, n) : array[0]; };

    2、_.initial():返回数组中除了最后一个元素外的其他全部元素。 在arguments对象上特别有用。传递 n参数将从结果中排除从最后一个开始的n个元素

    _.initial = function(array, n, guard) {
      //n == null--1;
      //n != null;guard == true--1
      //n != null;guard != true--n return slice.call(array, 0, array.length - ((n == null) || guard ? 1 : n)); };
    //倒序指定的n个元素
    slice.call(array, Math.max(array.length-n, 0));
    

    3、_.last():返回array(数组)的最后一个元素。传递 n参数将返回数组中从最后一个元素开始的n个元素,跟first相反

    _.last = function(array, n, guard) {
        if ((n != null) && !guard) {
          return slice.call(array, Math.max(array.length - n, 0));
        } else {
          return array[array.length - 1];
        }
      };
    

    4、_.rest():返回数组中除了第一个元素外的其他全部元素。传递 index 参数将返回除了第一个元素到第index元素以外剩余的所有元素,会输出index个元素,跟_.initail相反。underscore不同版本,会函数的定义都不同。

    _.rest = _.tail = function(array, index, guard) {
       //guard=true,index无效;slice()输出包含start 
      return slice.call(array, (index == null) || guard ? 1 : index); };

    5、_.compact:返回能被转为true的元素 filter返回满足验证的元素

    _.compact = function(array) {
        return _.filter(array, function(value){ return !!value; });
      };
    

    6、_.flatten:多维数组转为一维数组 reduce对元素进行迭代,迭代器返回memo,shallow参数用于控制合并深度, 当shallow为true时, 只合并第一层, 默认进行深层合并

    _.flatten = function(array, shallow) {
        return _.reduce(array, function(memo, value) {
          //shallow=true,concat value:链接两个或多个数组
          if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
          //value为值,数组自增赋值
          memo[memo.length] = value;
          return memo;
        }, []);
      };
    

    7、_.without():返回一个删除所有values值的 array副本。与_.difference类似;前者的第二参数为元素,后者的第二参数为数组

    _.without = function(array) {
       // slice.call(arguments, 1)返回从第二个开始的所有参数 return _.difference(array, slice.call(arguments, 1)); };

    8、_.uniq():返回 array去重后的副本, 使用 === 做相等测试.
    如果您确定 array 已经排序, 那么给 isSorted 参数传递 true值, 此函数将运行的更快的算法.

    _.uniq = _.unique = function(array, isSorted, iterator) {
        //如何判断第二个参数是isSorted还是iterator,先判断iterator是否存在。
        //如果iterator存在,则会根据它创建一个新的数组,以它做对比
        var initial = iterator ? _.map(array, iterator) : array;
        var results = [];
        //length=2,则一定有序
        if (array.length < 3) isSorted = true;
        //把数组元素归为统一的值
        _.reduce(initial, function (memo, value, index) {
          //isSorted=true:当前元素与最后一个元素比较:如果不相等的话,有可能跟之前的元素相等呀(已经是有序了,所以相等的都是相邻的)
          //isSorted=false:使用include来检查
          if (isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) {
            //memo存得时iterator的值
            memo.push(value);
            //results存的是array的值
            results.push(array[index]);
          }
          return memo;
        }, []);
        return results;
      };
    

    9、_.union():与uniq方法作用一致, 不同之处在于union允许在参数中传入多个数组

    _.union = function() {
        //true:只合并一层
        return _.uniq(_.flatten(arguments, true));
      };
    

    10、_.intersection():获取多个数组的交集元素;

    _.intersection = _.intersect = function(array) {
        var rest = slice.call(arguments, 1);
        //filter:满足条件的返回
        //先对array去重,uniq只会处理第一个array
        //rest是剩余的,需要比较的
        return _.filter(_.uniq(array), function(item) {
          return _.every(rest, function(other) {
            return _.indexOf(other, item) >= 0;
          });
        });
      };
    

    11、_.difference():返回当前数组中与指定数据不相等的差异数据,第二个参数为array,

    _.difference = function(array) {
      //只合并一层 var rest = _.flatten(slice.call(arguments, 1), true); return _.filter(array, function(value){ return !_.include(rest, value); }); };

    12、_.zip():将每个数组的相同位置的数据作为一个新的二维数组返回, 返回的数组长度以传入参数中最大的数组长度为准, 其它数组的空白位置使用undefined填充,zip方法应该包含多个参数, 且每个参数应该均为数组

    _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
    => [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]] 
    _.zip = function() {
        var args = slice.call(arguments);
        //pluck:获取对象数组的属性值
        var length = _.max(_.pluck(args, 'length'));
        var results = new Array(length);
        //arguments都是数组
        for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
        return results;
      }; 

    13、_.indexof():搜索一个元素在数组中首次出现的位置, 如果元素不存在则返回 -1,搜索时使用 === 对元素进行匹配 

    _.indexOf = function(array, item, isSorted) {
    	if (array == null) return -1;
    	var i, l;
    	if (isSorted) {
    	  i = _.sortedIndex(array, item);
          //写的真好,以后不用if了 return array[i] === item ? i : -1; } if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item); for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i; return -1; };

    14、_.lastIndexOf():返回一个元素在数组中最后一次出现的位置, 如果元素不存在则返回 -1,搜索时使用 === 对元素进行匹配,与indexof相反

    _.lastIndexOf = function(array, item) {
        if (array == null) return -1;
        if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
        var i = array.length;
        while (i--) if (i in array && array[i] === item) return i;
        return -1;
      };
    

    15、_.range():创建数组,内容为连续数字

    _.range = function(start, stop, step) {
        if (arguments.length <= 1) {
          //如果start=undefined,则stop为0;如果start不为undefined,则stop=start
          stop = start || 0;
          start = 0;
        }
        step = arguments[2] || 1;
    
        var len = Math.max(Math.ceil((stop - start) / step), 0);
        var idx = 0;
        var range = new Array(len);
    
        while(idx < len) {
          range[idx++] = start;
          start += step;
        }
    
        return range;
      };
    

      

  • 相关阅读:
    手把手教你利用create-nuxt-app脚手架创建NuxtJS应用
    初识NuxtJS
    webpack打包Vue应用程序流程
    用选择器代替表格列的筛选功能
    Element-UI
    Spectral Bounds for Sparse PCA: Exact and Greedy Algorithms[贪婪算法选特征]
    Sparse Principal Component Analysis via Rotation and Truncation
    Generalized Power Method for Sparse Principal Component Analysis
    Sparse Principal Component Analysis via Regularized Low Rank Matrix Approximation(Adjusted Variance)
    Truncated Power Method for Sparse Eigenvalue Problems
  • 原文地址:https://www.cnblogs.com/wang-jing/p/4737605.html
Copyright © 2011-2022 走看看