forked from RameshRM/Javascript-GroupBy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_groupBy.js
46 lines (38 loc) · 1.04 KB
/
_groupBy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Array.prototype._group = function (groupKey) {
if (arguments === null || arguments.length === 0) {
return this;
}
var groupByArgs = arguments;
var srcList = this;
var fieldValue = null;
var groupResult = new Object();
$.each(srcList, function (key, value) {
for (var i in groupByArgs) {
var argsKey = groupByArgs[i];
var fieldValue = value[argsKey];
if (groupResult[fieldValue] == null) {
groupResult[fieldValue] = srcList._filter(argsKey, fieldValue);
}
}
})
console.log(groupResult);
return groupResult;
}
Array.prototype._filter = function(filterBy, filterValue){
return $.grep(this, function(key, value){
return key[filterBy] === filterValue;
});
}
Array.prototype._sort = function (sortBy) {
var outer, inner, temp;
var srcList = this;
for (outer = srcList.length - 1; outer > 0; outer--) {
for (inner = 0; inner < outer; inner++) {
if (srcList[inner][sortBy] > srcList[inner + 1][sortBy]) {
temp = srcList[inner];
srcList[inner] = srcList[inner + 1];
srcList[inner + 1] = temp;
}
}
}
}