skip to Main Content

I have an object, I need to parse and put in another object from format 1 to format 2.

Format 1:

let mydata=[{
    "_children": {
        "5": {
            "_children": {
                "7": {
                    "_children": {},
                    "id": "7",
                    "name": "subject11",
                    "parentID": 5
                },
                "10": {
                    "_children": {},
                    "id": "10",
                    "name": "subject12",
                    "parentID": 5
                }
            },
            "id": "5",
            "name": "subject1",
            "parentID": null
        },
        "6": {
            "_children": {
                "8": {
                    "_children": {},
                    "id": "8",
                    "name": "subject21",
                    "parentID": 6
                },
                "9": {
                    "_children": {},
                    "id": "9",
                    "name": "subject22",
                    "parentID": 6
                }
            },
            "id": "6",
            "name": "subject2",
            "parentID": null
        }
    }
}]

Format 2:

let endresult=[
    {"id":7,    "name":"subject11",   "parent":"subject1"},
    {"id":10,   "name":"subject12",   "parent":"subject1"}
    {"id":8,    "name":"subject21",   "parent":"subject2"},
    {"id":9,    "name":"subject22",   "parent":"subject2"},  
]

Do you have any idea?

2

Answers


  1. tldr:

    let mydata = [{
      "_children": {
          "5": {
              "_children": {
                  "7": {
                      "_children": {},
                      "id": "7",
                      "name": "subject11",
                      "parentID": 5
                  },
                  "10": {
                      "_children": {},
                      "id": "10",
                      "name": "subject12",
                      "parentID": 5
                  }
              },
              "id": "5",
              "name": "subject1",
              "parentID": null
          },
          "6": {
              "_children": {
                  "8": {
                      "_children": {},
                      "id": "8",
                      "name": "subject21",
                      "parentID": 6
                  },
                  "9": {
                      "_children": {},
                      "id": "9",
                      "name": "subject22",
                      "parentID": 6
                  }
              },
              "id": "6",
              "name": "subject2",
              "parentID": null
          }
      }
    }]
    
    const convertFormat = (data, parentName = null) => {
      let result = [];
      for (let key in data._children) {
        let item = data._children[key];
        if (Object.keys(item._children).length > 0) {
          result = result.concat(convertFormat(item, item.name));
        }
        if (item.parentID !== null) {
          result.push({ id: parseInt(item.id), name: item.name, parent: parentName });
        }
      }
      return result;
    }
    
    let endresult = convertFormat(mydata[0]);
    console.log(endresult);
    // [
    //   {"id":7,    "name":"subject11",   "parent":"subject1"},
    //   {"id":10,   "name":"subject12",   "parent":"subject1"},
    //   {"id":8,    "name":"subject21",   "parent":"subject2"},
    //   {"id":9,    "name":"subject22",   "parent":"subject2"},
    // ]
    
    Login or Signup to reply.
  2. You could get the entries and a flat array as result.

    const
        data = { _children: { 5: { _children: { 7: { _children: {}, id: "7", name: "subject11", parentID: 5 }, 10: { _children: {}, id: "10", name: "subject12", parentID: 5 } }, id: "5", name: "subject1", parentID: null }, 6: { _children: { 8: { _children: {}, id: "8", name: "subject21", parentID: 6 }, 9: { _children: {}, id: "9", name: "subject22", parentID: 6 } }, id: "6", name: "subject2", parentID: null } } },
        flat = object => Object
            .values(object)
            .flatMap(({ _children, ...node }) => [
                ...(node.parentID === null ? [] : [node]),
                ...(_children ? flat(_children) : [])
            ]),
        result = flat(data._children);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search