Added map-reduce and find methods to list

This commit is contained in:
Anatoliy Chakkaev 2013-04-03 23:52:19 +04:00
parent 97513a43e0
commit f56cbaa150
1 changed files with 14 additions and 2 deletions

View File

@ -115,12 +115,24 @@ if (_) {
}); });
} }
['slice', 'forEach', 'filter'].forEach(function (method) { ['slice', 'forEach', 'filter', 'reduce', 'map'].forEach(function (method) {
var slice = [].slice; var slice = [].slice;
List.prototype[method] = function () { List.prototype[method] = function () {
return Array.prototype[method].apply(this.items, slice.call(arguments)); return Array.prototype[method].apply(this.items, slice.call(arguments));
}; };
});; });
List.prototype.find = function(pattern, field) {
if (field) {
var res;
this.items.forEach(function(o) {
if (o[field] == pattern) res = o;
});
return res;
} else {
return this.items[this.items.indexOf(pattern)];
}
};
List.prototype.toObject = function() { List.prototype.toObject = function() {
return this.items; return this.items;