拼接错误的元素
我有一系列评论
。其中一些注释实际上是 comments
中其他节点的子注释。每个 comment
都有一个 num_comments
、parent_id
和 id
属性。我知道当评论数量大于 0 时,评论就有子评论。
我想将子评论放入其父评论中,并从数组中删除子评论。外部 for 循环完成后,comments
数组中不应有子注释,并且每个子注释都会移至其父注释的 subcomments
数组中。
问题是,运行此代码后,comments
中的每个项目都被删除,我得到:
无法读取未定义的属性“item”
(这是 comments
为空的结果。)
这是我遇到问题的代码:
for comment in comments
if comment.item.num_comments > 0
comment.item.subcomments = [] unless comment.item.subcomments
for comment_second in comments # Goes through a second time to find subcomments for the comment
if comment_second.item.parent_id == comment.item.id
comment.item.subcomments.push(comment_second)
comments.splice(comments.indexOf(comment_second), 1)
编辑:
下面的答案没有行不通,但这绝对是朝着正确方向迈出的一步。我对代码进行了一些修改,我认为发生的情况是 temp_comment.item.subcomment
s 没有被定义为数组。 这会导致一个错误,不允许它被推送。这并不能解释什么没有从数组中删除。
temp_comments = comments.slice(0)
for comment in comments
for comment_second in comments
temp_comment = temp_comments[temp_comments.indexOf(comment)]
temp_comment.item.subcomements = [] unless temp_comment.item.subcomments?
if comment_second.item.parent_id == comment.item.id
temp_comment.item.subcomments.push(comment_second)
temp_comments.splice(temp_comments.indexOf(comment_second), 1)
comments = temp_comments
我收到与
第二次编辑之前相同的错误消息:
错误实际上是 [] 不是函数
I have an array of comments
. Some of these comments are actually subcomments of other nodes within comments
. Every comment
has a num_comments
, parent_id
, and id
attribute. I know a comment has subcomments when itss number of comments is greater than 0.
I want to put the subcomments inside it's parent comment, and remove the subcomment from the array. After the outer for loop has completed, there should be no child comments inside the comments
array, and each of the child comments is moved into it's parent comment's subcomments
array.
The issue is that after this code is run, every item in comments
is deleted, and I get:
Cannot read property 'item' of undefined
(Which is a result of comments
being empty.)
Here's the code that I'm having trouble with:
for comment in comments
if comment.item.num_comments > 0
comment.item.subcomments = [] unless comment.item.subcomments
for comment_second in comments # Goes through a second time to find subcomments for the comment
if comment_second.item.parent_id == comment.item.id
comment.item.subcomments.push(comment_second)
comments.splice(comments.indexOf(comment_second), 1)
Edit:
The answer below didn't work, but it was definitely a step in the right direction. I messed around with the code a bit, and what I think is happening is that temp_comment.item.subcomment
s is not being defined as an array.
Which causes an error that doesn't let it get pushed. What this doesn't explain is nothing is being removed from the array.
temp_comments = comments.slice(0)
for comment in comments
for comment_second in comments
temp_comment = temp_comments[temp_comments.indexOf(comment)]
temp_comment.item.subcomements = [] unless temp_comment.item.subcomments?
if comment_second.item.parent_id == comment.item.id
temp_comment.item.subcomments.push(comment_second)
temp_comments.splice(temp_comments.indexOf(comment_second), 1)
comments = temp_comments
I'm getting the same error message as before
2nd Edit:
The error is actually [] is not a function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑要循环访问的数组时,必须非常小心。如果您位于元素
i
上,并将其从数组中删除,那么现在您位于先前的元素i + 1
上。但随后循环递增,并且您跳过了最初的元素i + 1
。在这里,您处于两个嵌套循环中,两个循环都位于您正在修改的列表上,因此错误变得更加复杂。这是一些我相信可以实现您想要的功能的代码。
在这里,我们创建了一个临时数组(
comments.slice(0)
是数组的浅复制习惯用法)并修改了它而不是原始数组。编辑:我假设评论对象是为此设置的。要解决这个问题,请在拼接之前执行此操作:
You have to be very careful when editing an array that you are looping through. If you are on element
i
, and you remove it from the array, then now you are on what was previously elementi + 1
. But then the loop increments and you've skipped what was originally elementi + 1
. Here, you're in two nested loops, both over the list you're modifying, so the errors get much more complicated.Here's some code that I believe does what you want.
Here, we've created a temporary array (
comments.slice(0)
is a shallow copy idiom for arrays) and modified that instead of the original.Edit: I'd assumed that the comment objects were set up for this. To fix that, do this before the splicing:
我想你还在用 Javascript 思考。
这应该做同样的事情并且更清晰。
You are still thinking in Javascript I think.
This should do the same thing and be more clear.