javascript - Turning objects into an array of counts -
i'm trying wrap mind around problem need solved, don't seem anywhere. want done easier exemplify.
i have array of objects, looking this:
[ {"city_name": "new york", "visited": "2014-10-20"}, {"city_name": "new york", "visited": "2014-10-20"}, {"city_name": "new york", "visited": "2014-10-20"}, {"city_name": "new york", "visited": "2014-10-21"}, {"city_name": "new york", "visited": "2014-10-21"}, {"city_name": "stockholm", "visited": "2014-10-20"}, {"city_name": "stockholm", "visited": "2014-10-20"}, {"city_name": "stockholm", "visited": "2014-10-21"}, {"city_name": "stockholm", "visited": "2014-10-21"}, ]
now, want accomplish turn array following:
[ { "key": "new york", "values": [ ['2014-10-20', 3], // because there 3 visits in new york @ date ['2014-10-21', 2] // because there 2 visits in new york @ date ] }, { "key": "stockholm", "values": [ ['2014-10-20', 2], ['2014-10-21', 2] ] } ]
i tried using mapreduce function (from underscore.js) solve this, after failing generate output wanted, , same (failed) result few other for-tries, decided inquire here. maybe knows must done?
and, sorry horrible title. if you've got improve thought it, please comment (might help others reach question well)
if using underscore option:
var result = _.chain(arr).groupby('city_name').map(function (el, key) { homecoming { key: key, values: _.chain(el).countby('visited').pairs().value() } }).value();
javascript arrays
No comments:
Post a Comment