_.underscore examples

by Igor Cuckovic

JavaScript

//http://net.tutsplus.com/tutorials/javascript-ajax/getting-cozy-with-underscore-js/
//http://www.developerdrive.com/2012/04/an-introduction-to-underscore-js-%E2%80%93-part-1-arrays/
//http://www.developerdrive.com/2012/05/an-introduction-to-underscore-js-%E2%80%93-part-2-array-like-collections/

var scores = [84, 99, 91, 65, 87, 55, 72, 68, 95, 42], 
topScorers = [], scoreLimit = 90;
 
//topScorers = _.select(scores, function(d){ return d > scoreLimit;});
topScorers = _(scores).select(function(score){ return score > scoreLimit;});
 
//console.log(topScorers);

var Tuts = [{name : 'NetTuts', niche : 'Web Development'}, {name : 'WPTuts', niche : 'WordPress'}, {name : 'PSDTuts', niche : 'PhotoShop'}, {name : 'AeTuts', niche : 'After Effects'}];

var selected = _.pluck(Tuts, "name");
var keys = _.each(Tuts, function (item) {return _.keys(item)});
var values = _.values(Tuts);

// console.log(keys);

///////////////////////////////////////////////
 
//var names = _(Tuts).pluck('name').map(function (value){return value + '+'});
var numbers = _(scores).map( function (value) {return Math.round(value / 2);} )
//console.log(numbers);

//////////////////////
var Scores = [95, 82, 98, 78, 65];
var hasPassed = _(Scores).all(function (value) {return value>50; });
 //console.log(hasPassed);

/////////////////////////
var tens = _.range(100, 0, -10); 
//console.log(tens);
/////////////////////////////

var myCollection = {
one: ["one", "two", "three", "four", "five"],
two: [1, 2, 3, 4, 5]
};
var myCollection2 = [
{ name: "Dan", age: 33 },
{ name: "Fred", age: 44 }
];

_.each(myCollection, function(item) {
//console.log(item.length);
});