underscore-keypath
key-path mechanism extensions for underscore (mixin).
underscore-keypath let you access JavaScript objects and arrays with keypath easily.
var foo = {
bar : {
name : "Cool!"
},
scores : [55, 27, 100, 33]
};
_(foo).valueForKeyPath("bar.name");
_(foo).setValueForKeyPath("bar.name", "BAR");
_(foo).valueForKeyPath("scores.@max");
Install
NodeJS
$ npm install underscore-keypath
var _ = require("underscore-keypath");
or you may want to use origianl underscore:
var _ = require("underscore");
require("underscore-keypath");
in this case, please install "underscore" first.
$ npm install underscore
$ npm install underscore-keypath
Otherwise, underscore-keypath extends separated underscore in sandbox.
Meteor
$ meteor add jeeeyul:underscore-keypath
Front-end
$ bower install underscore-keypath
Or just download underscore-keypath.js manually.
Examples
var list = [{
name : "foo",
info : {
favoriteColor : "red",
age : 20
}
},{
name : "bar",
info : {
favoriteColor : "green",
age : 17
}
},{
name : "zar",
info : {
favoriteColor : "red",
age : 34
}
}];
_(list).pluckByKeyPath("info.age");
_(list).sortByKeyPath("info.age");
_(list).groupByKeyPath("info.favoriteColor");
See API Document