Question

I have an array of comments. Here's a sample 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"
                        }
                    ]
                }
            ]
        }
    ]
}

How do I flatten the replies into a single array of replies for each comment, so it looks like this instead:

{
    "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"
        }
    ]
}
Was it helpful?

Solution

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, []));

Output

[ { 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' } ]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top