根据underscore精简的函数(数组篇)

这里将underscore.js内的函数进行精简,指在帮助理解或者快速回忆起underscore.js的原理,更深入的了解函数式编程。

本文基于underscore.js1.8.3版本,参考手册为
http://www.css88.com/doc/underscore/

这里将精简underscore中提供的针对数组的操作,部分API已经在《根据underscore精简的函数(集合篇)》中体现,不再赘述。

_.initial

var initial = function (array, n) {
    return Array.prototype.slice.call(array, 0, Math.max(0, array.length - (n == null ? 1 : n)));
};
var a =initial([5, 4, 3, 2, 1],2);

console.log(a);

运行结果

[ 5, 4, 3 ]

_.rest

var rest=function(arr,n){
    return Array.prototype.slice.call(arr,n==null?1:n);
};
var a =rest([5, 4, 3, 2, 1],2);

console.log(a);

运行结果

[ 3, 2, 1 ]

_.first

var first=function(arr,n){
    if(arr==null||arr.length<1) return void 0;
    if(n==null) return arr[0];
    return initial(arr,arr.length-n);
};
var a =first([5, 4, 3, 2, 1],2);

console.log(a);

运行结果

[ 5, 4 ]

_.flatten

var isArray=function(obj){
    return Object.prototype.toString.call(obj)==='[object Array]';
};

var flatte=function(input,shallow,strict,startIndex){
    var output=[],idx=0;
    for(var i=startIndex||0,length=input.length;ivar value=input[i];
        if (isArray(value)){
            if(!shallow) value=flatte(value,shallow,strict);
            var j=0,len=value.length;
            output.length+=len;
            while(jelse if(!strict){
            output[idx++]=value;
        }
    }
    return output;
};
var flatten = function (array, shallow) {
    return flatte(array, shallow, false);
};
var a =flatten([1, [2], [3, [[4]]]]);
var b =flatten([1, [2], [3, [[4]]]],true);

console.log(a);
console.log(b);

运行结果

[ 1, 2, 3, 4 ]
[ 1, 2, 3, [ [ 4 ] ] ]

_.uniq

var uniq=function(arr,isSorted){
    var result=[],seen=[];
    for(var i=0,length=arr.length;ivar value=arr[i],
            computed=value;
        if(isSorted){
            if(!i||seen!==computed) result.push(value);
            seen=computed;
        }else if(result.indexOf(value)==-1){
            result.push(value);
        }
    }
    return result;
};
var a=uniq([1,1,1,1,2,2,2,3,3]);

console.log(a);

运行结果

[ 1, 2, 3 ]

_.intersection

var intersection=function(array){
    var result=[];
    var argsLength=arguments.length;
    for(var i=0,length=array.length;ivar item=array[i];
        if (!(result.indexOf(item)==-1)) continue;
        for(var j=1;jif(arguments[j].indexOf(item)==-1) break;
        }
        if(j===argsLength) result.push(item);
    }
    return result;
};
var a=intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);

console.log(a);

运行结果

[ 1, 2 ]

_.difference

var difference = function(array) {
    var rest = flatte(arguments, true, true, 1);
    return filter(array, function(value){
        return (rest.indexOf(value)==-1);
    });
};

var a=difference([1, 2, 3, 4, 5], [5, 2, 10]);
console.log(a);

运行结果

[ 1, 3, 4 ]

_.unzip

var unzip=function(array){
    var length=array.length;
    var result=Array(length);
    for(var index=0;indexreturn result;
};
var names = ['moe', 'larry', 'curly'];
var ages = [18, 23, 30];
var sexes = ['male', 'female', 'male'];
var students = unzip([names, ages, sexes]);

console.log(students);

运行结果

[ [ 'moe', 18, 'male' ],
  [ 'larry', 23, 'female' ],
  [ 'curly', 30, 'male' ] ]

_.compact

var compact=function(array){
    return filter(array,Boolean);
};
var a=compact([0, 1, false, 2, '', 3]);

console.log(a);

运行结果

[ 1, 2, 3 ]

_.object

var object=function(list,values){
    var result={};
    for (var i=0,length=list.length;iif(values){
            result[list[i]]=values[i];
        }else{
            result[list[i][0]]=list[i][1];
        }
    }
    return result;
};

var a=object(['moe', 'larry', 'curly'], [30, 40, 50]);
var b=object([['moe', 30], ['larry', 40], ['curly', 50]]);
console.log(a);
console.log(b);

运行结果

{ moe: 30, larry: 40, curly: 50 }
{ moe: 30, larry: 40, curly: 50 }

_.range

var range=function(start,stop,step){
    if(stop==null){
        stop=start||0;
        start=0;
    }
    if(!step){
        step=stop1:1;
    }
    var length=Math.max(Math.ceil((stop-start)/step),0);
    var range=Array(length);
    for(var idx=0;idxreturn range;
};
var a=range(0,30,5);
console.log(a);

运行结果

[ 0, 5, 10, 15, 20, 25 ]

_.chunk

var chunk=function(arr,count){
    if(count==null||count<1) return [];
    var result=[];
    var i=0,length=arr.length;
    while(iArray.prototype.slice.call(arr,i,i+=count));
    }
    return result;
};

var a=chunk([1,2,3,4,5,6,7], 2);
console.log(a);

运行结果

[ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ]

你可能感兴趣的:(前端)