【问题标题】:Splicing the wrong element(s)拼接错误的元素
【发布时间】:2012-01-19 01:38:07
【问题描述】:

我有一个comments 数组。其中一些 cmets 实际上是 comments 内其他节点的子 cmets。每个comment 都有一个num_commentsparent_idid 属性。我知道当评论的 cmets 数量大于 0 时,评论有 subcmets。

我想将子注释放在它的父注释中,并从数组中删除子注释。外部 for 循环完成后,comments 数组中应该没有子 cmets,并且每个子 cmets 都被移动到其父注释的 subcomments 数组中。

问题是,运行此代码后,comments 中的每个项目都被删除,我得到:

无法读取未定义的属性“项目”

(这是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.subcomments 没有被定义为一个数组。 这会导致一个不允许它被推送的错误。这没有解释的是没有从数组中删除任何内容。

    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

我收到与以前相同的错误消息

第二次编辑:

错误其实是[] is not a function

【问题讨论】:

    标签: arrays loops coffeescript client-side-scripting


    【解决方案1】:

    在编辑循环遍历的数组时,您必须非常小心。如果您在元素 i 上,并且将其从数组中删除,那么现在您在之前的元素 i + 1 上。但是随后循环增加,您已经跳过了最初的元素i + 1。在这里,您处于两个嵌套循环中,都在您正在修改的列表上,因此错误变得更加复杂。

    这里有一些代码,我相信它可以满足您的需求。

    temp_comments = comments.slice(0)
    for comment in comments
      for comment_second in comments
        if comment_second.item.parent_id == comment.item.id
          comment.item.subcomments.push(comment_second)
          temp_comments.splice(temp_comments.indexOf(comment_second), 1)
    comments = temp_comments
    

    在这里,我们创建了一个临时数组(comments.slice(0) 是数组的浅拷贝习语)并对其进行了修改而不是原始数组。

    编辑:我假设评论对象是为此设置的。要解决此问题,请在拼接之前执行此操作:

    for comment in comments
        comment.item.subcomments = []
    

    【讨论】:

    • @Jarred 你有错误的行号吗?我怀疑没有任何东西被删除,因为它在完成之前出错并停止运行,所以 cmets = temp_cmets 永远不会发生。
    • @Jarred 你确定吗?该行没有函数调用,所以我很确定它不可能得到is not a function 错误。
    【解决方案2】:

    我认为你仍在考虑使用 Javascript。

    这应该做同样的事情并且更清楚。

    # Add subcomments to all comments that have them
    for comment in comments when comment.item.num_comments > 0
      comment.item.subcomments = (sub for sub in comments when sub.item.parent_id == comment.item.id)
    
    # Filter out comments that have parents
    comments = (comment for comment in comments when !comment.item.parent_id)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-26
      • 2017-06-02
      • 2015-02-22
      • 1970-01-01
      • 2021-07-28
      • 2014-08-26
      • 2013-06-13
      • 1970-01-01
      相关资源
      最近更新 更多