合并评论中的回复。回复也可以有回复

时间:2014-04-26 07:53:08

标签: javascript lodash

我有一个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"
        }
    ]
}

1 个答案:

答案 0 :(得分:3)

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' } ]