1

This is the Object sample I'm given:

Input :

{ "People": [ { "id": "12", "parentId": "0", "text": "Man", "level": "1", "children": null }, { "id": "6", "parentId": "12", "text": "Boy", "level": "2", "children": null }, { "id": "7", "parentId": "12", "text": "Other", "level": "2", "children": null }, { "id": "9", "parentId": "0", "text": "Woman", "level": "1", "children": null }, { "id": "11", "parentId": "9", "text": "Girl", "level": "2", "children": null } ] }

I want to transform it to a JSON format like this:

{
    "People": [
        {
            "id": "12",
            "parentId": "0",
            "text": "Man",
            "level": "1",
            "children": [
                {
                    "id": "6",
                    "parentId": "12",
                    "text": "Boy",
                    "level": "2",
                    "children": null
                },
                {
                    "id": "7",
                    "parentId": "12",
                    "text": "Other",
                    "level": "2",
                    "children": null
                }   
            ]
        }
}

Any Ideas/Help would be appreciated

7
  • 2
    Can you show us what your attempt looked like? Commented Nov 16, 2018 at 11:47
  • Wait a minute.. Commented Nov 16, 2018 at 11:57
  • I tried implementing the solution of this question: stackoverflow.com/questions/18017869/… but didn't work after editing :/ @SpoonMeiser Commented Nov 16, 2018 at 12:12
  • I almost finished it... it's taking too long.. :) Commented Nov 16, 2018 at 12:12
  • I think that the first example is the best approach you can use. It's the more efficient way of representing the tree. Commented Nov 16, 2018 at 12:13

2 Answers 2

1

You could use an object for the reference to either child or parent and collect the children and parents to get only the person for the root.

var data = { John: "James", Samar: "Michel", Albert: "Michel", Michel: "James", James: "Sarah" },
    parents = new Set,
    children = new Set,
    references = {},
    result;

Object
    .entries(data)
    .forEach(([child, parent]) => {
        references[child] = references[child] || [];
        references[parent] = references[parent] || [];
        references[parent].push({ [child]: references[child] });
        parents.add(parent);
        children.add(child);
    });

result = [...parents]
    .filter(p => !children.has(p))
    .map(p => ({ [p]: references[p] }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

OMG this seemingly simple stuff took so long

const users = {
        "John": "James",
        "Samar": "Michel",
        "Albert": "Michel",
        "Michel": "James",
        "James": "Sarah"
    }
    
    const findRoots = () => Object.keys(users).filter(k => !(users[k] in users)).map(k => users[k])
    
    const findSubordinates = (boss) => Object.keys(users).filter(k => users[k] === boss)
    
    const traverseBoss = (boss) => {
        let subs = findSubordinates(boss)
    
        let subsCollection = []
    
        subs.forEach(s => {
            subsCollection.push({
                [s]: traverseBoss(s)
            })
        })
    
        return subsCollection
    }
    
    
    const result = {}
    findRoots().forEach(root => result[root] = traverseBoss(root))
    
    console.log(result)

1 Comment

@NurbolAlpysbayev I'm sorry but I have a new account and I cannot upvote :/

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.