在项目中的javascript代码编写中,我们总是会遇见诸如循环,计算等等问题。
有的时候我们可能还需要一些工具性的代码,来帮助我们完成某一类的问题。
underscore.js就帮我们很好解决了上面的问题,并且它的源代码看起来也不是很难。
官网地址:underscore
underscore的语法和jquery类似,使用_;
在underscore.js中,它提供了对Collections,Array,Object等的一些简单操作,并且它也提供了一些工具,来简化我们的代码。
再对Collections的操作中,提供了each,map,reduce,find,filter,contains等操作。
再对Arrays的操作中,提供了without,union等操作。
在练习中,由于_.where方法的例子不是很完全,所以我就自己写了个例子。
var list = [{author:"Shakespeare",title:"china"},
{author:"Shakespeare",year:1611,title:"china"},
{name:"zhongguo"}
];
var newArr = _.where(list, {author: "Shakespeare", year: 1611});
console.log(newArr);
这样的话这个_.where的作用就很明显了,
where的作用就是返回一个包含参数中所有key-value的值的数组
Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in properties.