0

i have the following array of dictionaries with the one below as an example of a single dictionary

{
        "id": 80,
        "name": "Low Level Topic",
        "questions": [
            {
                "id": 23,
                "shortName": null,
                "name": "Some question"
            },
            {
                "id": 24,
                "shortName": null,
                "name": "Some question 2"
            }
        ],
        "parentTopic": {
            "id": 10,
            "name": "Higher Level Topic",
            "parentTopic": {
                 "id": 1,
                 "name": "Top Level Topic",
                 "parentTopic": null
                }
            }
        }
    }

and i would like to reverse the tree to end up with the following

{
    "id": 1,
    "name": "Top Level Topic",
    "childTopic": {
        "id": 10,
        "name": "Higher Level Topic",
        "childTopic": {
            "id": 80,
            "name": "Low Level Topic",
            "questions": [
                {
                    "id": 23,
                    "shortName": null,
                    "name": "Some question"
                 },
                 {
                     "id": 24,
                     "shortName": null,
                     "name": "Some question 2"
                 }
        ],
        }
    } 
}

i have been trying to find an easy way to regenerate/restructure the data, but was not successful. I would appreciate if anyone could help

2 Answers 2

1

You can use simple recursion and it won't mutate the original obj

let obj = {"id":80,"name":"Low Level Topic","questions":[{"id":23,"shortName":null,"name":"Some question"},{"id":24,"shortName":null,"name":"Some question 2"}],"parentTopic":{"id":10,"name":"Higher Level Topic","parentTopic":{"id":1,"name":"Top Level Topic","parentTopic":null}}};

const buildReverseObj = (originalObj, currObj) => {
  const copyObj = { // make copy of original obj
    ...originalObj,
  };
  const parentTopic = copyObj.parentTopic; // get the parentTopic to process in next step
  delete copyObj.parentTopic; // parentTopic should be removed 

  const childTopic = { // make a child topic in every step it consists of older contents and newer contents
    ...copyObj,
    ...currObj
  };
  if (!parentTopic) return childTopic; // when parentTopic = null we are done

  return buildReverseObj(parentTopic, { childTopic });
};
console.log(buildReverseObj(obj, {}));

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

1 Comment

Thanks Asraf, this worked nicely :)
0

This problem is essentially reversing a linked list, and changing its structure a little.

let data = {"id":80,"name":"Low Level Topic","questions":[{"id":23,"shortName":null,"name":"Some question"},{"id":24,"shortName":null,"name":"Some question 2"}],"parentTopic":{"id":10,"name":"Higher Level Topic","parentTopic":{"id":1,"name":"Top Level Topic","parentTopic":null}}};

// WARNING: this code MUTATES the object

// previous topic, current topic, new tail as 'tail'
let prev = null, curr = data, tail = data.parentTopic;

while (curr) {
    next = curr.parentTopic;
    delete curr.parentTopic; // rename parentTopic
    curr.childTopic = prev;  // to childTopic
    prev = curr;
    curr = next;
}

delete tail.childTopic.childTopic; // delete since it's null

data = prev; // assign new head to data

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

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.