Last active
January 1, 2016 20:59
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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