lodash和underscore

Lodash 作为 Underscore 的后继者,除了对 Underscore 现有 API 功能使用上进行扩充外,更是添加了不少令人难忘的 API,在性能上也更为出彩,而且还能根据需要构建自己的子集方法。所以他两推荐用lodash

lodash和Ramda

var _ = require("lodash");
var R = require("ramda");

var companies = [
{ name: "tw", since: 1993 },
{ name: "pucrs", since: 1930 },
{ name: "tw br", since: 2009 }
];

var r1 = _(companies).chain()
.filter(function(c) {
return c.name.split(" ")[0] === "tw";
})
.map(function(c) {
return {
name: c.name.toUpperCase(),
since: c.since
};
})
.sortBy(function(c) {
return c.since;
})
.reverse()
.value();

console.log("with lodash:", r1);

var r2 = R.compose(
R.reverse,
R.sortBy(R.prop("since")),
R.map(R.over(R.lensProp("name"), R.toUpper)),
R.filter(R.where({ name: R.test(/^tw/) }))
)(companies);

console.log("with ramda:", r2);

显然Ramda写起来更加简洁,因为他是函数式编程,但是理解比较麻烦,有一定学习成本。