我有这个功能:
exports.getRules = function(user) {
if (!user) {
return Promise.resolve(null);
} else {
return Rule.findAllPr({
'status': 'published',
'type': 'price'
}).then(function(rules) {
return _.zipObject(rules.map(function(r) {
var skus;
skus = _.zipObject(r.sizeIds.map(function(s) {
return [s, r.discount];
}));
return console.log(skus);
}));
});
}
};
我正在尝试返回一个组合的skus数组,目前我正在获得两个数组
{ 'SA40-MRE0': 5,
'SA40-MRE1': 5,
'SA40-MRE2': 5,
'SA40-MRE3': 5,
'SA40-MRE4': 5,
'SA40-MRE5': 5 }
{ 'N50P-IB0': 15,
'N50P-IB1': 15,
'N68Z-BL4': 15 }
数据来自mongo:
{
"__v" : 11,
"_id" : ObjectId("5627c993dc8c59e63f4b87f6"),
"discount" : 5,
"from" : ISODate("2015-10-20T23:00:00Z"),
"sizeIds" : [
"SA40-MRE0",
"SA40-MRE1",
"SA40-MRE2",
"SA40-MRE3",
"SA40-MRE4",
"SA40-MRE5"
],
"status" : "published",
"to" : ISODate("2015-10-31T00:00:00Z"),
"type" : "price"
}
{
"to" : ISODate("2015-11-02T00:00:00Z"),
"from" : ISODate("2015-10-22T23:00:00Z"),
"type" : "price",
"_id" : ObjectId("562a50e0da1312415f73568c"),
"status" : "published",
"sizeIds" : [
"N50P-IB0",
"N50P-IB1",
"N68Z-BL4"
],
"discount" : 15,
"__v" : 0
}
是否有更好的方法来编写此代码,基本上我有两个或更多mongo文档,对于每个文档,我想提取sizeIds并将它们存储在skus数组中。
任何建议都非常感谢。
答案 0 :(得分:0)
使用_.extend
exports.getRules = (user) ->
if !user
Promise.resolve null
else
discountCache = {}
Rule.findAllPr(
'status': 'published'
'type': 'price')
.then (rules) ->
_.zipObject rules.map (r) ->
skus = _.zipObject(r.sizeIds.map((s) ->
[
s
r.discount
]
))
extended = _.extend(discountCache, skus)
.then (skus) ->
discountCache