2
我想應用組和排序對象。組和排序對象與Underscore.js
var emplData = [{
"company": "companyA",
"title": "positionA",
"office": "Boston",
"first-name": "Mike",
"last-name": "Bloom",
"profile-url": "url"
}, {
"company": "companyA",
"title": "positionD",
"office": "Amsterdam",
"first-name": "Adam",
"last-name": "Smart",
"profile-url": "url"
}, {
"company": "companyB",
"title": "positionB",
"office": "Toronto",
"first-name": "Tina",
"last-name": "Carmichael",
"profile-url": "url"
}, {
"company": "companyB",
"title": "positionA",
"office": "Chicago",
"first-name": "Seth",
"last-name": "Big",
"profile-url": "url"
}, {
"company": "companyC",
"title": "positionC",
"office": "St. Louis",
"first-name": "Carla",
"last-name": "Elsas",
"profile-url": "url"
}]
我喜歡組由company
的數據,然後排序辦公室(升序)。我正在使用underscore.js進行分組,它會返回類似於下面的對象。現在
var grpData = _.groupBy(emplData, 'company');
{
CompanyA:[{object1}, {object2} etc.],
CompanyB: [{object1}, {object2} etc.],
CompanyC: [{object1}, {object2} etc.]
}
分組的陣列內的每個對象都有屬性,包括辦公室,我不能讓我需要通過它的數據進行排序的結果,所以我們有一個升序排列。
我試過下面的方法,但似乎沒有工作。
var srtData = _.sortBy(grpData , function (i) {
$(i).each(function (i2, val) {
return val.office;
});
});
任何人都知道這個解決方案嗎?
這是一個很好且快速的方法!感謝薩米 – habibg