Underscore: https://www.css88.com/doc/underscore/
循环
_each:forEach
无返回,在循环内进行一些操作
numbers.forEach((item,index) => {console.log(item)})
_map:map
有返回,返回新数组,不改变原数组
[1,2,3].map(x=>x*x)
_reduce:reduce
reduceRight:从右边开始
[0, 1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) =>{
return accumulator + currentValue;
});
// 参数说明:当前累加值,当前值,当前索引,原数组
_.filter:filter
var ages = [32, 33, 16, 40];
ages2 = ages.filter(x=>{return x>=18}); //返回新数组,原数组不改变
_some:some 一旦找到符合条件的就退出返回true
_every:every 全部符合条件才返回true
关于for of 、for in 、forEach等
1、for of用于原生具备Iterato接口的数据结构Array、Map、Set、String、TypeArray、argument、NodeList对象和其组合,用于普通对象会报错
entries(默认)
keys(键名)
values(键值)
2、for in 用于普通对象,取的值是键名,值是字符串,“1”、“2”等。会遍历包括原型链。
3、forEach,break和return不可以用,map也不能。类数组要转换成数组再用forEach。
数组
_.rest(array, [index]) :...
扩展运算符...
//...arr当做散的传进去
console.log(1, ...[2, 3, 4], 5)// 1 2 3 4 5
Math.max(...[14, 3, 77]) ;
array.push(...items);
[...arr1, ...arr2, ...arr3] ;//合并数组
_.find:find 找出第一个符合条件的数组成员
findIndex 找出第一个符合成员的位置
[1,2,3,4].find(x = > x>3)
_.pluck(list, propertyName)
pluck也许是map最常使用的用例模型的简化版本,即萃取数组对象中某属性值,返回一个数组
let result=objArray.map(a=>a.foo);