2016-05-06

Backbone 集合(collection)中提到的 underscore 方法

Backbone and underscore

each 遍历(传入一个对象或数组)

  1. var oEach = {name:'Ming', age:12}
    
  2. _.each(oEach,function(value, index){
    
  3.   console.log([index] + ':' + [value]); //name:Ming   age:12
    
  4. });

map 遍历 (传入一个对象或数组)

  1. var map = [1, 2, 3, 4, 8, 6, 5];
    
  2. var mapObj = {one:1, Two:2, Three:3};
    
  3.  
  4. //将数组中的每个值*3
    
  5. _.map(map, function(number){
    
  6.   console.log(number * 3);  //3,6,9,15,24,18,15
    
  7. });
    
  8.  
  9. //将每个对象的值遍历*3
    
  10. _.map(mapObj, function(index,value){
    
  11.   console.log(value + ':' + index * 3); //one:3 two:6 three:9
    
  12. });

reduce 将数组内数字相加求和

  1. var reduces = [5, 6, 10];
    
  2. var oX = function(one, two) {
    
  3.   return one + two;
    
  4. };
    
  5. console.info(_.reduce(reduces, oX));    //21
    
  6. console.info(_.reduce(reduces, oX, 2)); //23

//reduceRight 从右侧开始组合元素

  1. var oReduceRight = [[2, 5], [9, 6], [3, 1]];
    
  2. var oReduceRightFn = function(a, b) {
    
  3.   return a.concat(b);
    
  4. };
    
  5. console.log(_.reduceRight(oReduceRight, oReduceRightFn, [])); //[3, 1, 9, 6, 2, 5]

find 遍历查找(遍历,查找满足条件的值,找到匹配的元素,函数将立即返回,不会遍历整个list)

  1. var list = [1, 2, 3, 4];
    
  2. _.find(list, function(nb){
    
  3.   if (nb % 2 == 0) {
    
  4.     return console.log(nb); //2(满足条件后立即返回,所以不会遍历到4)
    
  5.   }
    
  6. });

filter 遍历 list 中的每个值,返回满足条件检测到的值

  1. var list = [1, 2, 3, 4];
    
  2. _.filter(list, function(nb){
    
  3.   if (nb % 2 == 0) {
    
  4.     return console.log(nb); //2 和 4(偶数都满足条件)
    
  5.   }
    
  6. });

reject 与 filter 相反,遍历 list 中的每个值,返回不满足条件检测到的值

  1. var list = [1, 2, 3, 4];
    
  2. _.reject(list, function(nb){
    
  3.   if (nb % 2 == 0) {
    
  4.     return console.log(nb); //1 和 3(reject 与 filter 相反)
    
  5.   }
    
  6. });

every 当数组中的所有值为 true 时,则返回 true。否则返回 false

  1. var oEvery = [true, 1, null, 'yes'];
    
  2. console.info(_.every(oEvery));  //false

some 当数组中某个值满足 true 则返回 true,否则则返回 false。(其中 ‘yes’ 是字符串,是 true)

  1. var oSome = [null, 0, 'yes', false];
    
  2. console.log(_.some(oSome)); //true

contains 数组中是否包含该值

  1. var oContains = [1, 2, 3, 4];
    
  2. console.log(_.contains(oContains, 3));    //true(因为包含 3 所以返回 true)
    
  3. console.info(_.contains(oContains, 8));   //false(不包含 8 所以返回 false)

invoke 将数组中的数组进行排序

  1. var oInvoke = [[1, 5, 3,11],[9, 8, 7]];
    
  2. console.log(_.invoke(oInvoke, 'sort')); //[[1, 11, 3, 5], [7, 8, 9]]

max 遍历返回最大值

  1. var oMax = [{name:'tom',age:12},{name:'lili',age:11},{name:'going',age:99}];
    
  2. var oMaxfn = function(e){
    
  3.   return e.age;
    
  4. };
    
  5. console.info(_.max(oMax, oMaxfn));  //age 最大值的那个对象 {name:'going',age:99}

min 遍历返回最小值

  1. var oMin = [2, 6, 9];
    
  2. console.log(_.min(oMin)); //2(最小值是2)

sortBy 使用某种方式进行排序

  1. var oSortBy = [1, 3, 5, 7, 9];
    
  2. var oSortByObj = [{name:'c',age:7},{name:'a',age:6},{name:'b',age:5}];
    
  3. var oSortByFn = function(number){
    
  4.   return Math.sin(number);
    
  5. };
    
  6. console.info(_.sortBy(oSortBy, oSortByFn)); //5 3 9 7 1
    
  7. console.log(_.sortBy(oSortByObj, 'name'));  //a b c(按照对象值进行排序)

groupBy 将数组中的值,按照指定方式进行分类

  1. var oGroupBy = ['one','two','three'];
    
  2. var oGroupByNb = [1.2, 1.5, 2, 2.3];
    
  3. var oGroupByNbFn = function(number) {
    
  4.   return Math.floor(number);
    
  5. };
    
  6. console.log(_.groupBy(oGroupBy,'length'));          //3: ['one','two'] 5: ['three']
    
  7. console.info(_.groupBy(oGroupByNb, oGroupByNbFn));  //1: [1.2, 1.5] 2: [2, 2.3]

indexBy 通过键值返回对象排序

  1. var oIndexBy = [{name:'c',age:7},{name:'a',age:6},{name:'b',age:5}];
    
  2. console.log(_.indexBy(oIndexBy, 'age'));  // {'5':{name:'b',age:5},'6':{name:'a',age:6},'7':{name:'c',age:7}}

countBy 返回符合条件值的数量(计算数量)

  1. var oCountBy = [1, 2, 3, 4, 5, 6, 7];
    
  2. var countFn = function(e) {
    
  3.   return e % 2 == 0 ? 'even' : 'odd';
    
  4. };
    
  5. console.log(_.countBy(oCountBy, countFn));  //{odd:4, even:3}

shuffle 随机打乱数组排序

  1. var oShuffle = [1, 2, 3, 4, 5, 6];
    
  2. console.log(_.shuffle(oShuffle)); //每次刷新随机打乱排序

sample 从数组中随机获取数值或指定随机获取[N]个值

  1. var oSample = [1, 2, 3, 4, 5, 6];
    
  2. console.log(_.sample(oSample));     //从数组中随机获取一个数值
    
  3. console.info(_.sample(oSample, 4)); //从数组中随机获取4个数值生成新数组

toArray 将对象转换为数组

  1. var oToArray = {a:0,b:1,c:2,d:3};
    
  2. console.log(_.toArray(oToArray)); //[0, 1, 2, 3]

size 获得数组或对象的 length

  1. var oSzie = {a:0,b:1,c:2,d:3};
    
  2. var oSzieArray = [1, 2, 3, 4, 5, 6];
    
  3. console.log(_.size(oSzie));       //4
    
  4. console.info(_.size(oSzieArray)); //6

partition 拆分一个数组,为两个数组。第一个数组满足自己所定义的条件

  1. var oPartition = [1, 2, 3, 4, 5, 6];
    
  2. var isOdd = function(e) {
    
  3.   return e % 2 != 0;
    
  4. };
    
  5. console.log(_.partition(oPartition, isOdd));  //[[1, 3, 5], [2, 4, 6]]

underscore 针对 Array 的操作方法

first 返回数组中的第一或前[N]个值

  1. var oFirst = [1, 2, 3, 4, 5, 6];
    
  2. console.info(_.first(oFirst));    //1
    
  3. console.log(_.first(oFirst, 3));  //[1, 2, 3]

initial 返回除最后一个的所有数组,或指定返回除[N]个后返回

  1. var oInitial = [1, 2, 3, 4, 5, 6];
    
  2. console.log(_.initial(oInitial));     //[1, 2, 3, 4, 5]
    
  3. console.info(_.initial(oInitial, 2)); //[1, 2, 3, 4]

last 返回最后一个数组值或指定返回后[N]个值

  1. var oLast = [1, 2, 3, 4, 5, 6];
    
  2. console.log(_.last(oLast));     //6
    
  3. console.info(_.last(oLast, 4)); //[3, 4, 5, 6]

without 删除数组中指定的值,并返回删除后的结果

  1. var oWithout = [1, 2, 3, 4, 5, 6];
    
  2. console.log(_.without(oWithout, 1, 5)); // [2, 3, 4, 6]

difference 类似于without,但返回的值来自array参数数组,并且不存在于other 数组。

  1. var oDifference = [1, 2, 3, 4, 5, 6];
    
  2. console.log(_.difference(oDifference,[1,4,6])); //[2, 3, 5]

indexOf 返回 value 在该 array 中的索引值,如果 value 不存在 array 中就返回-1。

  1. var oIndexOf = [1, 2, 3, 4, 5, 6];
    
  2. console.log(_.indexOf(oIndexOf, 4));  //3

lastIndexOf 返回 value 在该 array 中的从最后开始的索引值,如果value不存在 array 中就返回-1。

  1. var oLastIndexOf = [1, 2, 3, 4, 2, 6];
    
  2. console.info(_.lastIndexOf(oLastIndexOf, 2)); //4

underscore Obj 对象

isEmpty 判断该对象或数组是否为空,返回 true 或 false

  1. var oIsEmpty = [1, 2, 3, 4, 5, 6];
    
  2. var oIsEmptyObj = {name:'a',age:12};
    
  3. console.log(_.isEmpty(oIsEmpty));     //false
    
  4. console.info(_.isEmpty(oIsEmptyObj)); //false
    
  5. console.log(_.isEmpty({}));           //true
    
  6. console.info(_.isEmpty([]));          //true

underscore 链式语法

chain 封装对象

  1. var oChain = [{name:'c',age:7},{name:'a',age:6},{name:'b',age:5}];
    
  2. var oChainObj = _.chain(oChain);
    
  3. console.info(oChainObj);  //封装后的对象
    
  4.  
  5. //value 获取封装对象中的值
    
  6. console.log(oChainObj.value());