Skip to content

Instantly share code, notes, and snippets.

@TravelingTechGuy
Last active January 1, 2016 20:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravelingTechGuy/8200987 to your computer and use it in GitHub Desktop.
Save TravelingTechGuy/8200987 to your computer and use it in GitHub Desktop.
Returns an array of one property from each item in a JSON array, sorted by name, regardless of case. This sample uses LoDash (can be used with Underscore)
//0. given:
var _ = require('lodash');
var items = [
{
name: 'dfsdfd',
value: null
},
{
name: 'Abc',
value: null,
anotherValue: 'b'
},
{
name: 'X',
value: null,
OneMoreField: 'ccc'
}
];
//1. get all names, sorted, case insensitive
var arr = _.sortBy(_.flatten(items, 'name'), function(name) { return name.toLowerCase(); }); //arr == ['Abc', 'dfsdfd','X']
//2. get the item with a name in the array
var abc = _.find(items, {name: arr[0]}); //abc == {name: 'ABC', value: null, anotherValue: 'b'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment