[英]error in Javascript in storing the array value
output of 输出
result[0].brand_id = ["59df686fe30af41be049417f","59df68eca39cf51b40c566ab"]
and i want to store the value find by these id in single storage 'brand' 我想将这些ID找到的值存储在单个存储“品牌”中
and my code is 我的代码是
var data = {};
data['brands'] = [];
async.each(result[0].brand_id, function (brand,callback) {
Brand.findById(brand,function(err,result){
if(err){
callback("there is an error");
}
//console.log(result.brand_name);
data['brands'] = {
brand: result.brand_name,
dosage: result.dosage_id
};
console.log(data); // it is correct but but shows two single storage
});
callback();
},function (err,data) {
if(err){
console.log(err);
}
console.log(data); // it show output undefined
});
I want my output as 我希望我的输出为
{ brands:
[ { brand: 'a', dosage: ["59df686ee30af41be049417e"] },
{ brand: 'b', dosage: ["59df68eca39cf51b40c566aa"] } ] }
I am stuck here,please solve this. 我被困在这里,请解决此问题。
You'll have to create data['brand']
as an array and will have to push in values inside it. 您必须将
data['brand']
创建为数组,并且必须将值推入其中。
A sample solution: 样本解决方案:
if(!data['brand']) data['brand'] = []; // to check if it is the first time you are inserting inside data['brand'], in which case it needs to be initialized.
data['brand'].push({
brand: result.brand_name,
dosage: [result.dosage_id]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.