[Lodash] Quick start.

var people = [
  {name: "Rank", age: 40},
  {name: "Bob", age: 35},
  {name: "Gegg", age: 30},
  {name: "Leate", age: 18},
  {name: "Matti", age: 34},
  {name: "Low", age: 23},
  {name: "Cock", age: 23}
];

 

var result = _(people).sortBy('age').first(3).value();
console.log(result);

 

console.log(_(people).pluck('name').sort().value()); //only care about the name
//But here one thing should be carefully, it first latter is not uppercase, then it will always come after the uppcase after sorting. For example "Bob" is "bob"
["Bob", "Cock", "Gegg", "Leate", "Low", "Matti", "Rank"] 
[ "Cock", "Gegg", "Leate", "Low", "Matti", "Rank", "bob"] 

 

console.log(_(people).max('age').value());

console.log(_(people).min('age').value());

 

console.log(_(people).filter({age:23}).value()); //Get people age only 23

 

More: https://egghead.io/lessons/core-javascript-introduction-to-lodash

https://lodash.com/docs

posted @ 2014-10-10 20:26  Zhentiw  阅读(238)  评论(0)    收藏  举报