我有一个comments数组。这是一个示例comment

{
    "author": "John",
    "text": "Hi there",
    "replies":
    [
        {
            "author": "Henry",
            "text": "Hi John, did you get me that paper?",
            "replies":
            [
                {
                    "author": "John",
                    "text": "Which one?",
                    "replies":
                    [
                        {
                            "author": "Henry",
                            "text": "Never mind. Let's catch up later"
                        },
                        {
                            "author": "Frank",
                            "text": "The analysis sheet we talked about at the last meeting man!",
                            "replies":
                            [
                                {
                                    "author": "John",
                                    "text": "Oh that! Let me get back to you by the end of the day"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "author": "Barry",
            "text": "Call me about that last years report please when you find the time",
            "replies":
            [
                {
                    "author": "John",
                    "text": "About 5 good?",
                    "replies":
                    [
                        {
                            "author": "Barry",
                            "text": "Yes, thank you"
                        }
                    ]
                }
            ]
        }
    ]
}


如何将每个评论的回答统一为单个回答数组,因此看起来像这样:

{
    "author": "John",
    "text": "Hi there",
    "replies":
    [
        {
            "author": "Henry",
            "text": "Hi John, did you get me that paper?"
        },
        {
            "author": "John",
            "text": "Which one?"
        },
        {
            "author": "Henry",
            "text": "Never mind. Let's catch up later"
        },
        {
            "author": "Frank",
            "text": "The analysis sheet we talked about at the last meeting man!"
        },
        {
            "author": "John",
            "text": "Oh that! Let me get back to you by the end of the day"
        },
        {
            "author": "Barry",
            "text": "Call me about that last years report please when you find the time"
        },
        {
            "author": "John",
            "text": "About 5 good?"
        },
        {
            "author": "Barry",
            "text": "Yes, thank you"
        }
    ]
}

最佳答案

function get_replies(currentObject, result) {
    result.push({
        author: currentObject.author,
        text: currentObject.text
    });
    if (currentObject.replies) {
        for (var i = 0; i < currentObject.replies.length; i += 1) {
            get_replies(currentObject.replies[i], result);
        }
    }
    return result;
}

console.log(get_replies(data, []));


输出量

[ { author: 'John', text: 'Hi there' },
  { author: 'Henry', text: 'Hi John, did you get me that paper?' },
  { author: 'John', text: 'Which one?' },
  { author: 'Henry', text: 'Never mind. Let\'s catch up later' },
  { author: 'Frank',
    text: 'The analysis sheet we talked about at the last meeting man!' },
  { author: 'John',
    text: 'Oh that! Let me get back to you by the end of the day' },
  { author: 'Barry',
    text: 'Call me about that last years report please when you find the time' },
  { author: 'John', text: 'About 5 good?' },
  { author: 'Barry', text: 'Yes, thank you' } ]

关于javascript - 合并评论中的回复。回复也可以有回复,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23308242/

10-11 12:26