Added map-reduce and find methods to list
This commit is contained in:
parent
97513a43e0
commit
f56cbaa150
16
lib/list.js
16
lib/list.js
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue