arrays - 循环内循环数组
问题描述
基于这个Mongoose 嵌入式文档/DocumentsArrays id ,我尝试在循环内的循环中实现条件。最后,新的“tosave”评论对象应该包含来自 BlogPost 基于“where a_id = b_id”的_id。
我使用节点、快递和猫鼬。我有一个包含多个博客帖子的多行表单,一个博客帖子可以有多个评论。
在后端,我有 2 个数组 -> 博客帖子和评论,如下所示:
BlogPosts: [{ a_id: '1', name: 'name1' },
{ a_id: '2', name: 'name2' },
{ a_id: '3', name: 'name3' }],
Comments: [{ b_id: '1', name: 'comment for BlogPost name1' },
{ b_id: '1', name: 'other comment for BlogPost name1' },
{ b_id: '3', name: 'comment for BlogPost name3' }],
我需要每个评论中的博客文章文档中的 _id,以在评论和博客文章之间引用。
我像这样遍历 BlogPosts:
var new_post_to_save = [];
for(var i=0; i<req.body.blogposts.length;i++) {
var newpost = new BlogPost();
console.log('blogpost nr : ' + [i] + ' has new _id: ' + newpost._id );
newpost.name = req.body.blogposts[i].name,
newpost.a_id = req.body.blogposts[i].a_id
new_post_to_save.push(newpost);
},
这工作正常。我得到一个新数组“new_post_to_save”,我可以轻松保存。现在我尝试在 BlogPosts 中循环,为每个评论提供来自新创建的“newpost”对象的 _id 字段。
我尝试了什么 - 但它失败了..它匹配所有博客帖子的所有评论。
var new_post_to_save = [];
var new_comment_to_save = [];
for(var i=0; i<req.body.blogposts.length;i++) {
var newpost = new BlogPost();
console.log('blogpost nr : ' + [i] + ' has new _id: ' + newpost._id );
newpost.name = req.body.blogposts[i].name,
newpost.a_id = req.body.blogposts[i].a_id
// here a loop to create x Comments with the _id from newpost and
// the condition: IF a_id = b_id
// then create new Comment and get die _id from his Blogpost
for(var f=0; f<req.body.comments.length;f++) {
if (newpost.a_id = req.body.comments[f].b_id)
console.log(' Yea ! Comment ' + [f] + ' matches Blogpost Nr. ' + [i]);
var newcomment = new CommentModel();
newcomment.id_newpost = newpost._id,
newcomment.name = req.body.comments[f].name,
newcomment.b_id = req.body.comments[f].b_id,
}; // end if
}; // end for
new_comment_to_save.push(newcomment);
new_post_to_save.push(newpost);
}, // end for
这是我在这里的第一个问题。
有任何想法吗 ?非常感谢
解决方案
我对猫鼬不熟悉,只是看你的代码,你的代码似乎有点错误:位置:new_comment_to_save.push(newcomment); 它应该在 if 中,因为只有当它们匹配时,您才希望添加它们..
var new_post_to_save = [];
var new_comment_to_save = [];
for(var i=0; i<req.body.blogposts.length;i++) {
var newpost = new BlogPost();
console.log('blogpost nr : ' + [i] + ' has new _id: ' + newpost._id );
newpost.name = req.body.blogposts[i].name,
newpost.a_id = req.body.blogposts[i].a_id
// here a loop to create x Comments with the _id from newpost and
// the condition: IF a_id = b_id
// then create new Comment and get die _id from his Blogpost
for(var f=0; f<req.body.comments.length;f++) {
if (newpost.a_id = req.body.comments[f].b_id)
console.log(' Yea ! Comment ' + [f] + ' matches Blogpost Nr. ' + [i]);
var newcomment = new CommentModel();
newcomment.id_newpost = newpost._id,
newcomment.name = req.body.comments[f].name,
newcomment.b_id = req.body.comments[f].b_id,
new_comment_to_save.push(newcomment);
}; // end if
}; // end for
new_post_to_save.push(newpost);
}, // end for
推荐阅读
- html - 单击元素后,div内的标题会移到外面
- react-native - 如何使用谷歌登录从 AWS cognito 获取访问令牌
- python - 将查询集作为参数传递给 Django 中的 __in?
- cron - Ansible cronjob - 为文件名添加时间戳
- python - CVXPY 在基本二次问题上随机失败
- c# - 如何使用 LINQ 将另一个列表元素的列表类型属性的元素组合到一个列表中
- vscode-extensions - 从 Markdown 预览中的 VS Code Extension API 打开文件
- docker - 如何列出 docker 中所有已知的根密钥(Docker Content Trust)
- python - 在pycharm中创建的gui没有运行?
- reactjs - React JS 在 useEffect 中没有读写到 localStorage 没有错误