Underscore.js是一个工具类,在不扩展js的情况下,有100多个工具函数。
集合类
1.each函数
遍历集合中的每个元素,无返回内容
_.each(list,iterator,[context]);
var arr = [1,2,3,4]; _.each(arr,function(value,index,array){ console.log(index+":"+value+":"+array+":"+this.name); },{name:"zhangsan"});
//0:1:1,2,3,4:zhangsan
//1:2:1,2,3,4:zhangsan
//2:3:1,2,3,4:zhangsan
//3:4:1,2,3,4:zhangsan
var obj = { name:"zhangsan", value:1 } _.each(obj,function(value,key,array){ console.log(key+":"+value+":"+array+":"+this.name); },{name:"zhangsan"});
//name:zhangsan:[object Object]:zhangsan
//value:1:[object Object]:zhangsan
2.map函数
遍历集合中的每个元素,根据iterator,返回一个新的集合
_.map(list,iterator,[context]);
var arr = [1,2,3,4]; var newArr = _.map(arr,function(value,index,array){ if(value > 2){ return value; } }); console.log(newArr); //[undefined, undefined, 3, 4] 返回一个和原数组等长的数组,不满足条件的会用undefined代替
var obj = { name:"zhangsan", value:1 } var newObj = _.map(obj,function(value,key,object){ if(value == 1){ return value; } }); console.log(newObj)
//[undefined, 1]
3.find函数
find会查找符合iterator迭代器中自定义函数规则的元素。如果查找到,则返回一个首个符合的元素
_.find(list,iterator,[context]);
var arr = [1,2,3,4]; var find = _.find(arr,function(value,index,array){ if(value == 2){ return value; } }) console.log(find); //2
4.filter函数
filter函数会找符合iterator迭代器中自定义函数规则的元素。将匹配到的元素返回一个数组
_.filter(list,iterator,[context]);
var arr = [1,2,2,3,3,4]; var find = _.filter(arr,function(value,index,array){ if(value == 2){ return value; } }) console.log(find); //[2,2]
5.max和min函数
_.max(list,[iterator],[context])
_.min(list,[iterator],[context]),查找list中最大或者最小的数,如果添加了iterator迭代器则先过滤,然后比较。
var arr = [1,2,2,3,3,4,5]; var max = _.max(arr,function(value,index,array){ if(value != 5){ return value; } }) console.log(max); var min = _.min(arr,function(value,index,array){ if(value != 1){ return value; } }) console.log(min); //4 //2
如果list为对象 例如:{name:"zhangsan",age:28,score:78}则自动过滤掉name属性
也可以指定对象中的某个属性来判断大小
var arr = [{ name:"zhangsan", age:18 },{ name:"lisi", age:19 },{ name:"wangwu", age:20 }] var max = _.max(arr,function(value,key,array){ return value.age; }) var min = _.min(arr,function(value,key,array){ return value.age; }) console.log(max); console.log(min);
//Object {name: "wangwu", age: 20}
//Object {name: "zhangsan", age: 18}
6.sortBy()
sortBy函数对列表或者对象进行排序,按升序排列
_.sortBy(list,iterator,[context])
var arr = [{ name:"zhangsan", age:32 },{ name:"lisi", age:41 },{ name:"wangwu", age:20 }] var newArr = _.sortBy(arr,function(value,key,array){ return value.age; }) for(var i=0;i<newArr.length;i++){ console.log(newArr[i].age); //20 //32 //41
7.groupBy()
groupBy函数,将List列表按迭代器自定义函数或者一个对象的属性对list进行分割成多个数组
var arr = [{ name:"zhangsan", age:32 },{ name:"lisi", age:41 },{ name:"wangwu", age:20 },{ name:"wangermazi", age:28 }] var newArr = _.groupBy(arr,function(value,key,array){ if(value.age >= 20 && value.age <30){ return "20组"; } if(value.age >= 30 && value.age <40){ return "30组"; } if(value.age >= 40 && value.age <50){ return "40组" } }) console.log(newArr); //Object {30组: Array[1], 40组: Array[1], 20组: Array[2]}