Created
August 15, 2014 13:42
-
-
Save rexxars/8ce51e1b2dec75275919 to your computer and use it in GitHub Desktop.
SortBy with deep object access
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
'use strict'; | |
function deep(obj, prop) { | |
var segs = prop.split('.'); | |
while (segs.length) { | |
obj = obj[segs.shift()]; | |
} | |
return obj; | |
} | |
module.exports = function(prop) { | |
return function(item) { | |
return deep(item, prop); | |
}; | |
}; | |
// Usage: | |
var deep = require('deep'); // `deep` being the above | |
_.sortBy(arr, deep('some.nested.key')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment