最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Group and Sort object with Underscore.js - Stack Overflow

programmeradmin2浏览0评论

I'm trying to apply group and sort on objects.

var emplData = [{
    "pany": "panyA",
    "title": "positionA",
    "office": "Boston",
    "first-name": "Mike",
    "last-name": "Bloom",
    "profile-url": "url"
}, {
    "pany": "panyA",
    "title": "positionD",
    "office": "Amsterdam",
    "first-name": "Adam",
    "last-name": "Smart",
    "profile-url": "url"
}, {
    "pany": "panyB",
    "title": "positionB",
    "office": "Toronto",
    "first-name": "Tina",
    "last-name": "Carmichael",
    "profile-url": "url"
}, {
    "pany": "panyB",
    "title": "positionA",
    "office": "Chicago",
    "first-name": "Seth",
    "last-name": "Big",
    "profile-url": "url"
}, {
    "pany": "panyC",
    "title": "positionC",
    "office": "St. Louis",
    "first-name": "Carla",
    "last-name": "Elsas",
    "profile-url": "url"
}]

I like to group the data by pany and then sort by office (ascending order). I'm using underscore.js to group and it returns similar to object below.

var grpData = _.groupBy(emplData, 'pany');

  {
    CompanyA:[{object1}, {object2} etc.],
    CompanyB: [{object1}, {object2} etc.],
    CompanyC: [{object1}, {object2} etc.]
  }

Now each object inside the grouped array has properties including office, I can't get the results I need to sort the data by it so we have an ascending order.

I have tried below method but doesn't seem to work.

var srtData = _.sortBy(grpData , function (i) {
    $(i).each(function (i2, val) {
        return val.office;
    });
});

Anyone know a solution for this?

I'm trying to apply group and sort on objects.

var emplData = [{
    "pany": "panyA",
    "title": "positionA",
    "office": "Boston",
    "first-name": "Mike",
    "last-name": "Bloom",
    "profile-url": "url"
}, {
    "pany": "panyA",
    "title": "positionD",
    "office": "Amsterdam",
    "first-name": "Adam",
    "last-name": "Smart",
    "profile-url": "url"
}, {
    "pany": "panyB",
    "title": "positionB",
    "office": "Toronto",
    "first-name": "Tina",
    "last-name": "Carmichael",
    "profile-url": "url"
}, {
    "pany": "panyB",
    "title": "positionA",
    "office": "Chicago",
    "first-name": "Seth",
    "last-name": "Big",
    "profile-url": "url"
}, {
    "pany": "panyC",
    "title": "positionC",
    "office": "St. Louis",
    "first-name": "Carla",
    "last-name": "Elsas",
    "profile-url": "url"
}]

I like to group the data by pany and then sort by office (ascending order). I'm using underscore.js to group and it returns similar to object below.

var grpData = _.groupBy(emplData, 'pany');

  {
    CompanyA:[{object1}, {object2} etc.],
    CompanyB: [{object1}, {object2} etc.],
    CompanyC: [{object1}, {object2} etc.]
  }

Now each object inside the grouped array has properties including office, I can't get the results I need to sort the data by it so we have an ascending order.

I have tried below method but doesn't seem to work.

var srtData = _.sortBy(grpData , function (i) {
    $(i).each(function (i2, val) {
        return val.office;
    });
});

Anyone know a solution for this?

Share Improve this question edited Jun 8, 2015 at 18:28 thefourtheye 240k53 gold badges465 silver badges500 bronze badges asked Jun 8, 2015 at 18:10 habibghabibg 1853 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Why not sort and then group? Then the result will be a collection grouped by pany name each holding sorted array of objects (asc. by office name)

var grpData = _.groupBy(_.sortBy(emplData, "office"), 'pany');
发布评论

评论列表(0)

  1. 暂无评论