Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function id(x) { return x; }
- Array.prototype.where = function(predicate) {
- var newArray=[];
- for (var i=0;i<this.length;i++) {
- var item=this[i];
- if(predicate(item)) newArray.push(item);
- }
- return newArray;
- }
- Array.prototype.choose = function(chooser) {
- var newArray=[];
- for (var i=0;i<this.length;i++) {
- var item=chooser(this[i]);
- if(item!=null) newArray.push(item);
- }
- return newArray;
- }
- Array.prototype.countBy=function(getKey) {
- if(getKey==undefined) getKey=id;
- var counts={};
- for(var i=0;i<this.length;i++) {
- var key=getKey(this[i]);
- var count=counts[key];
- counts[key]=(count==undefined)?1:count+1;
- }
- return counts;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement