Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created August 22, 2015 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/c5abab37fc19bbd9ebe0 to your computer and use it in GitHub Desktop.
Save chuck0523/c5abab37fc19bbd9ebe0 to your computer and use it in GitHub Desktop.
(function() {
var log = function(x) {console.log(x)}
var a = [1, 2, 5, 8, 42, 21];
var x;
x = _.groupBy(a, function(num) {
return num % 3;
});
//log(x);
var f = function(){};
var mix = [1, true, 30, 'hello', null, 'test', 0, /aaa/, [], {}, false, f];
//log(mix);
y = _.groupBy(mix, function(ele) {
return typeof ele;
});
//log(y);
x = _.countBy(a, function(num){
return num % 2 == 0 ? 'even' : 'odd' ;
});
//log(x);
//log('before sort : '+a);
x = _.sortBy(a);
//log('after sort : '+x);
var people = [
{
name : 'moe',
age : 40
},
{
name : 'larry',
age : 50
},
{
name : 'curly',
age : 60
}
];
x = _.sortBy(people, 'name');
//log(x);
// groupBy by pure js
var obj = {
1:[],
2:[],
0:[]
};
for(var i = 0; i < a.length; i+=1) {
var n = a[i] % 3;
switch(n){
case 1:
obj[1].push(a[i]);
break;
case 2:
obj[2].push(a[i]);
break;
case 0:
obj[0].push(a[i]);
break;
default:
obj['other'] = [a[i]];
}
}
//log(obj);
//countBy by pure js
var obj = {
even : 0,
odd : 0
};
for(var i = 0; i < a.length; i++) {
if( a[i] % 2 ) {
obj['even'] += 1;
} else {
obj['odd'] += 1;
}
}
//log(obj);
// sortBy by pure js
var a = [50, 1, 6, 21, 4, 18];
var tmp;
for(var i = 0, l = a.length; i < l; i++) {
for(var j = i+1; j < l; j++) {
if(a[i] > a[j]) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
//log(a);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment