循环内循环遍历数组

时间:2018-09-08 16:52:37

标签: arrays loops mongoose

基于此Mongoose embedded documents / DocumentsArrays id 我试图在循环内部的循环中实现条件。最后,新的“ tosave”注释对象应包含基于“ where a_id = b_id”的BlogPost的_id。

我使用node,express和mongoose。 我有一个包含多个BlogPost的多行表单,一个Blogpost可以有多个注释。

在后端,我有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' }],

我需要每个Comment内的Blogpost文档中的_id,以在Comment和Blogpost之间进行引用。

我像这样遍历BlogPost:

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内部循环,以使每个Comment都具有新创建的“ newpost”对象中的_id字段。

我尝试执行的操作-但失败了。它将所有评论与所有BlogPosts匹配。

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

这是我的第一个问题。

有什么想法吗?非常感谢

1 个答案:

答案 0 :(得分:0)

我不喜欢猫鼬,只是看着您的代码,似乎您在代码中有一些错误:位置: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