0

私は自分のcommentsArray内にネストされたコメントの多くのレベルを持っています

(これは API から取得され、変更できません)

commentsArray = [
    {
        author: "jeff"
        replies: [
            {
                author: "jeff"
                replies: [
                    {
                        author: "simon"
                    }
                ]
            },
            {
                author: "simon"
            }
        ]
    },
    {
        author: "simon"
    }
]

問題のオブジェクトに「返信」がある場合に自分自身を呼び出す再帰関数 dialogParse もあります。

dialogueParse = (comment) ->
    for child in comment
        if child.author is "simon"
            console.log "Simon was found.."

        if child.replies
            dialogueParse child

ただし、このコードは正しく動作していないようです。最初に関数を呼び出した後:

dialogueParse commentsArray

..何らかの理由で、最初のレベル (配列の最後) にあるものだけが見つかりました。

" simon " は 3 つの異なる場所で著者としてリストされています。

私はこれに数時間取り組んでいますが、どこにも行きません。どんな助けでも大歓迎です!:)

4

1 に答える 1

2

child.replies再帰の引数として渡すべきではありませんか?

次のように:

dialogueParse = (comment) ->
    for child in comment
        if child.author is "simon"
            console.log "Simon was found.."

        if child.replies
            dialogueParse child.replies
于 2013-01-13T05:11:51.363 に答える