JavaScript的:
Template.list.jobs = function() {
if(Session.get('currentIndustryOnet')) {
jobs = Jobs.find({onet: Session.get('currentIndustryOnet')}).fetch();
// Session.set('jobCount', jobs.count());
var cnt = _.groupBy(jobs, 'address')
var tmp = []
$.each(cnt, function(indx, val){
tmp[indx] = _.size(val);
});
return Pagination.collection(jobs);
} else {
jobs = Jobs.find()
Session.set('jobCount', jobs.count());
return Pagination.collection(jobs.fetch());
}
}
我正在尝试接受我的groupBy
,对其进行排序并获得前100名结果。
分组的数组如下所示:
{
"Adelphi, MD": [{
_id: "fv2xCQt6T37f36vGH"
address: "Adelphi, MD"
company: "General Technical Services, LLC"
dateacquired: "2014-1-16 9:41 PM"
jvid: "3cc86df5855c4a6599e595ad1a5b18e9321"
lat: ""
lng: ""
onet: "17-0000.00"
title: "Technician / Electrician"
url: "http://my.jobs/3cc86df5855c4a6599e595ad1a5b18e9321"
}]
}
数组中的每个键都是一个城市/州名,我想要计算每个数组,按计数排序然后进入前100名。
答案 0 :(得分:0)
假设我理解了这个问题,这将返回前100名的排序(最大到最小)数组:
var compareLengthOfSecondElement = function(a, b) {
return a[1].length < b[1].length;
}
var result = _.chain(data)
.groupBy('address')
.pairs()
.sort(compareLengthOfSecondElement)
.slice(0, 100)
.value();