这有效:
MyCollection.prototype.select = function (properties) {
var self = this;
return {
where: function (conditions) {
return _.chain(self.arrayOfObjects)
.where(conditions)
.map(function (result) {
return _.pick(result, properties);
})
.value();
}
};
};
Run Code Online (Sandbox Code Playgroud)
它允许我像这样查询我的集合:
var people = collection
.select(['id', 'firstName'])
.where({lastName: 'Mars', city: 'Chicago'});
Run Code Online (Sandbox Code Playgroud)
我希望能够编写这样的代码,但是:
MyCollection.prototype.select = function (properties) {
var self = this;
return {
where: function (conditions) {
return _.chain(self.arrayOfObjects)
.where(conditions)
.pick(properties);
.value();
}
};
};
Run Code Online (Sandbox Code Playgroud)
Lo-Dash文档将_.pick回调指定为"[callback](Function | ... string | string []):每次迭代调用的函数或要选择的属性名称,指定为单独的属性名称或属性名称数组." 这让我相信我可以提供属性数组,它将应用于arrayOfObjects符合条件的每个项目.我错过了什么?
它期望Object作为第一个参数,你给它一个Array.
Arguments
1. object (Object): The source object.
2. ...
3. ...
Run Code Online (Sandbox Code Playgroud)
我认为这是你能做的最好的事情:
MyCollection.prototype.select = function (properties) {
var self = this;
return {
where: function (conditions) {
return _.chain(self.arrayOfObjects)
.where(conditions)
.map(_.partialRight(_.pick, properties))
.value();
}
};
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2486 次 |
| 最近记录: |