如何将对象列表嵌套到另一个对象列表中以及如何优雅地进行?

How to nest a list of objects into another and how to do it elegantly?

我有两个对象来自两个 SQL 查询:postscomments。现在我想创建一个“帖子”对象,每个帖子中都嵌套了“评论”对象。如果我加入 SQL 中的数据,我会得到一个没有嵌套的平面对象。我需要它嵌套的想法。我在两个 for 循环中尝试了以下方法,但这没有用,因为 new_object_line 还没有注释键。

joined_object = Object.assign(new_object_line.comments, rows_comments[x])

原始帖子列表对象:

var posts =
{
   post1:
   {
       title: 'Title 1',
       id: id1
   }
   post2:
   {
       title: 'Title 2',
       id: id2
   }
   post3:
   {
       title: 'Title 3',
       id: id3
       
   }
}

原始评论列表对象:

var comments = 
{
    comment1: {
        text: 'aaa',
        belongs_to_post: id2
    }

    comment2: {
        text: 'bbb',
        belongs_to_post: id2
    }

    comment3: {
        text: 'ccc',
        belongs_to_post: id3
    }
}

包含评论的帖子结果对象 joined_object 应该如下所示:

var joined_object = 
    {
       post1:
       {
           title: 'Title 1',
           id: 1
       }
       post2:
       {
           title: 'Title 2',
           id: 2
           comments: 
           {

                comment1: {
                    text: 'aaa',
                    belongs_to_post: id2
                }
    
                comment2: {
                    text: 'bbb',
                    belongs_to_post: id2
                }
           }
       }
       
       post3:
       {
           title: 'Title 3',
           id: 3
           comments: 

           {
              comment3: {
                text: 'ccc',
                belongs_to_post: id3
              }
           } 
       }
    }

您的示例几乎完美无缺,但实际上您更有可能拥有一个 post 对象数组,如下所示:

const posts = [
   {
      "title":"Title 1",
      "id":1
   },
   {
      "title":"Title 2",
      "id":2,
      "comments":[
         {
            "text":"aaa",
            "belongs_to_post":2
         },
         {
            "text":"bbb",
            "belongs_to_post":2
         }
      ]
   },
   {
      "title":"Title 3",
      "id":3,
      "comments":[
         {
            "text":"ccc",
            "belongs_to_post":3
         }
      ]
   }
]

如您所见 - 您现在在包含 post 的对象上没有 属性,而只是 post 的列表(我假设更接近于您的数据库结果)。

现在,如果您想根据 id 获得 return 单个 post,您可以执行以下操作:

const postTwo = posts.filter(post => post.id === 2);

然后您可以轻松访问 post 2 上的评论:

const comments = postTwo.comments;

在此处查看 JSON 对象基础知识,以及嵌套对象的示例: https://www.w3schools.com/js/js_json_objects.asp

如果你想使用对象,你可以使用 lodash 之类的东西,我建议这样:

const { mapValues, filter, isEmpty } = require('lodash')

const joined = mapValues(posts, post => {
  const byPostId = { belongs_to_post: post.id }
  const relatedComments = filter(comments, byPostId)

  if (isEmpty(relatedComments)) {
    return post
  }

  return { ...post, comments: relatedComments }
})

console.log(joined)

您可以通过匹配 id:

使用 for..in 循环实现预期的输出

for(let o in posts){
    for(let c in comments){
        if(posts[o].id == comments[c].belongs_to_post){
            // (posts[o].comments ??={})[c] = comments[c]
               posts[o].comments = posts[o].comments || {};
               posts[o].comments[c] = comments[c];
        }
    };
}