0

I am trying to get data from a firebase database. I am getting the Object structure this way:

enter image description here

I want to be made this way:

enter image description here

Is there any elegant way of doing this in the frontend or is it better to make the changes in the tree structure in the database?

The only reason I have it in the current form, is that it makes it easier for me to form a tree hierarchy structure later on.

0

1 Answer 1

1

You just have to do it manually, but thankfully it's not too hard. If snap is the firebase snapshot you are getting that includes postId/... as it's child, then:

const results = [];
snap.forEach(child => {
    child.forEach(grandchild => {
        results.push({
            id: child.key,
            uid: grandchild.key,
            ...grandchild.val(),
        });
    });
});

// Do stuff with results...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.