评论中的嵌套评论
Nested comments in comments
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
}
]
}
我有一个从猫鼬那里得到的深层嵌套对象。
一个。我如何遍历这个深层嵌套的对象?我是否应该使用递归和循环,因为我似乎无法解决这个问题?我设法遍历到评论的末尾。我设法想出了如下所示的蛮力,但我有点被困在这里。
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。如何计算总评论数,本例为8条评论?
非常感谢。
你可以使用递归来完成。 (因为它有无限数量的层,所以写所有的嵌套循环并没有真正扩展)。
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))
或者,如果递归深度导致问题,您可以使用基于 stack/queue 的方法。
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))
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
}
]
}
我有一个从猫鼬那里得到的深层嵌套对象。
一个。我如何遍历这个深层嵌套的对象?我是否应该使用递归和循环,因为我似乎无法解决这个问题?我设法遍历到评论的末尾。我设法想出了如下所示的蛮力,但我有点被困在这里。
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。如何计算总评论数,本例为8条评论?
非常感谢。
你可以使用递归来完成。 (因为它有无限数量的层,所以写所有的嵌套循环并没有真正扩展)。
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))
或者,如果递归深度导致问题,您可以使用基于 stack/queue 的方法。
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))