Skip to content

Instantly share code, notes, and snippets.

@AllThingsSmitty
Last active August 29, 2015 14:15
Show Gist options
  • Save AllThingsSmitty/06523ace403e6b05bed0 to your computer and use it in GitHub Desktop.
Save AllThingsSmitty/06523ace403e6b05bed0 to your computer and use it in GitHub Desktop.
Simplifying JS functions with Lo-Dash
// Get a simple array of objects
var drinks = [
{ 'name': 'Coke', 'quantity': 2 },
{ 'name': 'Red Bull', 'quantity': 6 }
];
// Get all the drink names using the _.pluck() function
var currentDrinks = _.pluck(drinks, 'name');
console.log(currentDrinks);
// → ['Coke', 'Red Bull']
// Get the drink with the highest stock level using the _.max() function
var drinks,
currentDrinks,
maxQuantity;
drinks = [
{ 'name': 'Coke', 'quantity': 2 },
{ 'name': 'Red Bull', 'quantity': 6 }
];
currentDrinks = _.pluck(drinks, 'name');
console.log(currentDrinks);
maxQuantity = _.max(drinks, 'quantity');
console.log(maxQuantity);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment