【问题标题】:Nested comments in comments评论中的嵌套评论
【发布时间】:2021-09-09 13:34:18
【问题描述】:
 const post =   {
        "comments": [
            {
                "comments": [
                    {
                        "comments": [
                            {
                                "comments": [
                                    {
                                        "comments": [],
                                        "_id": "60d6ab9207c0a573786a9e65",
                                        "userId": "60c418582f7066090ced4a51",
                                        "content": "good post 3",
                                        "createdAt": "2021-06-26T04:22:42.337Z",
                                        "updatedAt": "2021-06-26T04:22:42.337Z",
                                        "__v": 0
                                    }
                                ],
                                "_id": "60d6962cee10aa73f820b974",
                                "userId": "60c418582f7066090ced4a51",
                                "content": "good post 2",
                                "createdAt": "2021-06-26T02:51:24.111Z",
                                "updatedAt": "2021-06-26T04:22:42.622Z",
                                "__v": 0
                            },
                            {
                                "comments": [],
                                "_id": "60d705784ab01c354cf7f445",
                                "userId": "60c15ac41ed8da1ab4efe7f3",
                                "content": "Comment deleted by User",
                                "createdAt": "2021-06-26T10:46:16.813Z",
                                "updatedAt": "2021-06-26T12:29:06.398Z",
                                "__v": 0
                            },
                            {
                                "comments": [],
                                "_id": "60d706febcba957b04406547",
                                "userId": "60c15ac41ed8da1ab4efe7f3",
                                "content": "yes it is a good post 1 from alexane Updated",
                                "createdAt": "2021-06-26T10:52:46.679Z",
                                "updatedAt": "2021-06-26T12:17:58.879Z",
                                "__v": 0
                            }
                        ],
                        "_id": "60d695b4ee10aa73f820b973",
                        "userId": "60c418582f7066090ced4a51",
                        "content": "good post 1",
                        "createdAt": "2021-06-26T02:49:24.426Z",
                        "updatedAt": "2021-06-26T12:30:44.872Z",
                        "__v": 0
                    }
                ],
                "_id": "60d68e32dff84439a4d3b191",
                "userId": "60c418582f7066090ced4a51",
                "content": "good post",
                "createdAt": "2021-06-26T02:17:22.625Z",
                "updatedAt": "2021-06-26T02:49:24.820Z",
                "__v": 0
            },
            {
                "comments": [
                    {
                        "comments": [],
                        "_id": "60d6c2d917d0b12be44742d2",
                        "userId": "60c418582f7066090ced4a51",
                        "content": "nice post 1",
                        "createdAt": "2021-06-26T06:02:01.420Z",
                        "updatedAt": "2021-06-26T06:02:01.420Z",
                        "__v": 0
                    }
                ],
                "_id": "60d6bebf17d0b12be44742d1",
                "userId": "60c418582f7066090ced4a51",
                "content": "nice post",
                "createdAt": "2021-06-26T05:44:31.436Z",
                "updatedAt": "2021-06-26T06:02:01.676Z",
                "__v": 0
            }
        ]
    }

我有一个从猫鼬那里得到的深层嵌套对象。

一个。如何遍历这个深层嵌套的对象?我应该使用递归和循环,因为我似乎无法解决这个问题吗?我设法遍历到 cmets 的末尾。我设法想出了如下蛮力的方法,但我有点卡在这里。

const findObject = (obj) => {
    for (let i = 0; i < obj.comments.length; i++) {
        const element = obj.comments[i];
        for (let y = 0; y < element.comments.length; y++) {
            const newElements = element.comments[y];
            for (let z = 0; z < newElements.comments.length; z++) {
                const newElementsZ = newElements.comments[z];
                console.log(newElementsZ)

            }
        }
    }
};

b. cmets的总数如何计算,本例为8 cmets?

非常感谢。

【问题讨论】:

  • 你可以使用递归,或者如果它很深并导致问题,使用队列/堆栈来模拟真正的递归调用。

标签: javascript node.js recursion mongoose nested-lists


【解决方案1】:

你可以使用递归来做到这一点。 (由于它的层数不定,编写所有嵌套循环并不能真正扩展)。

const post = { "comments": [{ "comments": [{ "comments": [{ "comments": [{ "comments": [], "_id": "60d6ab9207c0a573786a9e65", }], "_id": "60d6962cee10aa73f820b974", }, { "comments": [], "_id": "60d705784ab01c354cf7f445", }, { "comments": [], "_id": "60d706febcba957b04406547", } ], "_id": "60d695b4ee10aa73f820b973", }], "_id": "60d68e32dff84439a4d3b191", }, { "comments": [{ "comments": [], "_id": "60d6c2d917d0b12be44742d2", }], "_id": "60d6bebf17d0b12be44742d1", } ] }

function CountComment(data){
  let count = 0
  for(let c of data.comments){
    ++count
    count+=CountComment(c)
  }
  return count
}

console.log(CountComment(post))

如果递归深度导致问题,您可以使用基于堆栈/队列的方法。

const post = { "comments": [{ "comments": [{ "comments": [{ "comments": [{ "comments": [], "_id": "60d6ab9207c0a573786a9e65", }], "_id": "60d6962cee10aa73f820b974", }, { "comments": [], "_id": "60d705784ab01c354cf7f445", }, { "comments": [], "_id": "60d706febcba957b04406547", } ], "_id": "60d695b4ee10aa73f820b973", }], "_id": "60d68e32dff84439a4d3b191", }, { "comments": [{ "comments": [], "_id": "60d6c2d917d0b12be44742d2", }], "_id": "60d6bebf17d0b12be44742d1", } ] }

function CountComment(post){
  let nodes = [post]
  let count = 0
  while(nodes.length){
    let node = nodes.pop()
    ++count
    for(let c of node.comments)
       nodes.push(c)
  }
  return count-1 //remove post itself
}

console.log(CountComment(post))

【讨论】:

  • 顺便说一句,这里是单线function CountComment(data){ return post.comments.reduce((a,c)=&gt;a+CountComment(c)+1,0)}(我确实建议使用这个)
  • @desh 你当前的输入结构(const post一棵树(有名为comments的孩子),不太清楚你在说什么。
  • @desh 哦,我只是意识到您在比较(堆栈与队列),而不是(堆栈或队列与递归)。 (here)
  • @desh 我使用堆栈只是因为 javascript 没有高效(简单)的方式来实现队列。
  • @desh 堆栈将是深度优先搜索,而队列将是广度优先搜索。无论如何,这与计数节点无关。 (顺便说一句,正常的递归调用是 DFS)(我 猜想 DFS 通常会有更少的节点排队等待通常的帖子)
猜你喜欢
  • 2012-09-17
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 2021-03-04
相关资源
最近更新 更多