作者:田大才子2016-10-10 16:01分类: UnderscoreJs
本文所有文字解释都在代码中以注释的方式存在。
请复制代码到自己喜欢的文本编辑器中观看。
//1._.each(list, iteratee, [context]) //遍历list中的所有元素,按顺序用遍历输出每个元素。 _.each([1, 2, 3], function(key,value,list){ console.info(key + ":" + value) }); _.each({one: 1, two: 2, three: 3}, function(key,value,list){ console.info(key + ":" + value) }); //2._.map(list, iteratee, [context]) //通过转换函数(iteratee迭代器)映射列表中的每个值产生价值的新数组。 console.info(_.map([1, 2, 3], function(num){ return num * 3; })); console.info(_.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; })); console.info(_.map([[1, 2], [3, 4]],function(num,key){ return num[0]; })); //3._.reduce(list, iteratee, [memo], [context]) //参数0是memo的初始值 var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0); console.info(sum); //4._.reduceRight(list, iteratee, memo, [context]) //从右侧开始 var list = [[0, 1], [2, 3], [4, 5]]; var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []); console.info(flat); //5._.find(list, predicate, [context]) //找到第一个符合条件的值就返回 var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); console.info(even); //6._.filter(list, predicate, [context]) //返回所有符合条件的 var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); console.info(evens); //7_.where(list, properties) 。 //遍历一个list,返回符合条件的所有元素 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ] var ls = _.where(list, {age : '11'}) console.info(ls); //8._.findWhere(list, properties) //返回所有符合条件的第一个值 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ] var obj = _.findWhere(list, {age : '11'}) console.info(obj); //9._.reject(list, predicate, [context]) //返回所有不符合条件的,与filter相反 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ] var ls = _.reject(list, function(o){ return o.age == "12"; }); console.info(ls) //10._.every(list, [predicate], [context]) //数组中所有元素都符合条件,返回true,否则返回false var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ] console.info(_.every(list, function(o){ return o.age !== '111'; })); //11._.some(list, [predicate], [context]) //任何一个符合条件,就返回ture var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ] console.info(_.some(list, function(o){ return o.age === '12'; })); //12._.contains(list, value, [fromIndex]) //list中包换制定的value,就返回true var lo = {name:'tianqing1',age:'12'}; var list = [ lo, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; console.info(_.contains(list, lo)); //13._.invoke(list, methodName, *arguments) //这个方法有点难理解,不过看一下我给的例子应该会明白 //在list的每个元素上执行methodName方法。 //任何传递给invoke的额外参数,invoke都会在调用methodName方法的时候传递给它。 function tenX(x){ this.x = x; this.ten = function(){ this.x = 10 * this.x; return this; } }; var list = [ new tenX(1), new tenX(2), new tenX(3), new tenX(4) ] //每一个元素调用它自己的方法,然后返回自己 var arr = _.invoke(list, 'ten'); console.info(arr); //14._.pluck(list, propertyName) //萃取,获取某个对象集合中的某个属性,然后再返回成一个集合 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ] var ls = _.pluck(list, 'name'); console.info(ls); //15._.max(list, [iteratee], [context]) //返回集合中最大的一个 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var obj =_.max(list, function(o){ return o.age * Math.random(); }); console.info(obj); //16._.min(list, [iteratee], [context]) //返回最小的一个 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var obj =_.min(list, function(o){ return o.age * Math.random(); }); console.info(obj); //17._.sortBy(list, iteratee, [context]) //返回一个排序后的list拷贝副本。 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var ls = _.sortBy(list, function(o){ //倒序 return o.age *(-1); }); console.info(ls); console.info(list); //18._.groupBy(list, iteratee, [context]) //把一个集合分组为多个集合 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var ls = _.groupBy(list, function(o){ return o.age; }); console.info(ls); //19._.indexBy(list, iteratee, [context]) //和groupBy非常像,但是当你知道你的键是唯一的时候可以使用indexBy。 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var ls = _.indexBy(list, function(o){ return o.name; }); console.info(ls); //20._.countBy(list, iteratee, [context]) //排序一个列表组成一个组,并且返回各组中的对象的数量的计数。 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var ls = _.countBy(list, function(o){ return o.age % 2 == 0 ? 'even': 'odd'; }); console.info(ls); //21._.shuffle(list) //返回一个随机乱序的 list 副本 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var ls = _.shuffle(list); console.info(ls); //22._.sample(list, [n]) //从 list中产生一个随机样本。传递一个数字表示从list中返回n个随机元素。否则将返回一个单一的随机项。 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var ls = _.sample(list,2); console.info(ls) //23._.toArray(list) //把list(任何可以迭代的对象)转换成一个数组,在转换 arguments 对象时非常有用。 var arr = (function(){ return _.toArray(arguments).slice(0); })(1, 2, 3, 4); console.info(arr); //24._.size(list) //返回list的长度。 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; console.info(_.size(list)); //25._.partition(array, predicate) //拆分一个数组(array)为两个数组: 第一个数组其元素都满足predicate迭代函数, 而第二个的所有元素均不能满足predicate迭代函数。 var list = [ {name:'tianqing1',age:'12'}, {name:'tianqing2',age:'11'}, {name:'tianqing3',age:'13'}, {name:'tianqing4',age:'15'}, {name:'tianqing5',age:'11'} ]; var ls = _.partition(list, function(o){ return o.age > 12; }); console.info(ls);
发表评论:
昵称
邮件地址 (选填)
个人主页 (选填)
个人公众号,希望您的关注~
发表评论: