Ajai
Ajai

Reputation: 391

Dynamically forming array of object using given List

Inputs1

var aa = [
   {
      "id":1,
      "name":"ajai",
      "country":"india",
      "qualification":"BE",
      "age":23
   },
   {
      "id":2,
      "name":"aravindh",
      "country":"india",
      "qualification":"BCOM",
      "age":24
   },
   {
      "id":3,
      "name":"gopal",
      "country":"china",
      "qualification":"MA",
      "age":23
   },
   {
      "id":4,
      "name":"guvaliour",
      "country":"china",
      "qualification":"BE",
      "age":24
   },
   {
      "id":5,
      "name":"anavarthan",
      "country":"japan",
      "qualification":"BE",
      "age":25
   },
   {
      "id":6,
      "name":"veer",
      "country":"china",
      "qualification":"MA",
      "age":23
   },
   {
      "id":7,
      "name":"ramani",
      "country":"india",
      "qualification":"BE",
      "age":23
   },
   {
      "id":8,
      "name":"kumar",
      "country":"india",
      "qualification":"MBA",
      "age":23
   }
]

Input2

 var bb = ['country','age','qualification']

Based on the both inputs i want to form the array of object using this required format.

Required Format:

[
   {
      "id":"india",
      "children":[
         {
            "id":"23",
            "children":[
               {
                  "id":"BE",
                  "children":[

                  ],
                  "dataSet":[
                     {
                        "id":1,
                        "name":"ajai",
                        "country":"india",
                        "qualification":"BE",
                        "age":23
                     },
                     {
                        "id":7,
                        "name":"ramani",
                        "country":"india",
                        "qualification":"BE",
                        "age":23
                     }
                  ]
               },
               {
                  "id":"MBA",
                  "children":[

                  ],
                  "dataSet":[
                     {
                        "id":8,
                        "name":"kumar",
                        "country":"india",
                        "qualification":"MBA",
                        "age":23
                     }
                  ]
               }
            ]
         },
         {
            "id":"24",
            "children":[
               {
                  "id":"BCOM",
                  "children":[

                  ],
                  "dataSet":[
                     {
                        "id":2,
                        "name":"aravindh",
                        "country":"india",
                        "qualification":"BCOM",
                        "age":24
                     }
                  ]
               }
            ]
         }
      ]
   },
   {
      "id":"china",
      "children":[
         {
            "id":"23",
            "children":[
               {
                  "id":"MA",
                  "children":[

                  ],
                  "dataSet":[
                     {
                        "id":6,
                        "name":"veer",
                        "country":"china",
                        "qualification":"MA",
                        "age":23
                     },
                     {
                        "id":3,
                        "name":"gopal",
                        "country":"china",
                        "qualification":"MA",
                        "age":23
                     }
                  ]
               }
            ]
         },
         {
            "id":"24",
            "children":[
               {
                  "id":"BE",
                  "children":[

                  ],
                  "dataSet":[
                     {
                        "id":4,
                        "name":"guvaliour",
                        "country":"china",
                        "qualification":"BE",
                        "age":24
                     }
                  ]
               }
            ]
         }
      ]
   },
   {
      "id":"japan",
      "children":[
         {
            "id":"25",
            "children":[
               {
                  "id":"BE",
                  "children":[

                  ],
                  "dataSet":[
                     {
                        "id":5,
                        "name":"anavarthan",
                        "country":"japan",
                        "qualification":"BE",
                        "age":25
                     }
                  ]
               }
            ]
         }
      ]
   }
]

I have just tried to form this required format using the input 1 and input 2 but i struggled for a day.Please some one can help me out to solve this issue. I'm using this tree to form a grouping table in Angular 6.This is some what like recursive.

something i have tried like this

    var dd=[];
    var aa = [
      {
         "id":1,
         "name":"ajai",
         "country":"india",
         "qualification":"BE",
         "age":23
      },
      {
         "id":2,
         "name":"aravindh",
         "country":"india",
         "qualification":"BCOM",
         "age":24
      },
      {
         "id":3,
         "name":"gopal",
         "country":"china",
         "qualification":"MA",
         "age":23
      },
      {
         "id":4,
         "name":"guvaliour",
         "country":"china",
         "qualification":"BE",
         "age":24
      },
      {
         "id":5,
         "name":"anavarthan",
         "country":"japan",
         "qualification":"BE",
         "age":25
      },
      {
         "id":6,
         "name":"veer",
         "country":"china",
         "qualification":"MA",
         "age":23
      },
      {
         "id":7,
         "name":"ramani",
         "country":"india",
         "qualification":"BE",
         "age":23
      },
      {
         "id":8,
         "name":"kumar",
         "country":"india",
         "qualification":"MBA",
         "age":23
      }
   ];
   var bb = ['country','age','qualification'];
   for(var i=0;i<aa.length;i++){
      for(var j=0;j<bb.length;j++){
        dd.push(this.formingArr(aa[i][bb[j]]))
      }
     }
     console.log(dd);
  }

  formingArr(value){
    let obj={
      id:value,
      children:[],
      dataSet:[]
    }
      return obj;
  }

my Output:

[
  {
    "id": "india",
    "children": [],
    "dataSet": []
  },
  {
    "id": 23,
    "children": [],
    "dataSet": []
  },
  {
    "id": "BE",
    "children": [],
    "dataSet": []
  },
  {
    "id": "india",
    "children": [],
    "dataSet": []
  },
  {
    "id": 24,
    "children": [],
    "dataSet": []
  },
  {
    "id": "BCOM",
    "children": [],
    "dataSet": []
  },
  {
    "id": "china",
    "children": [],
    "dataSet": []
  },
  {
    "id": 23,
    "children": [],
    "dataSet": []
  },
  {
    "id": "MA",
    "children": [],
    "dataSet": []
  },
  {
    "id": "china",
    "children": [],
    "dataSet": []
  },
  {
    "id": 24,
    "children": [],
    "dataSet": []
  },
  {
    "id": "BE",
    "children": [],
    "dataSet": []
  },
  {
    "id": "japan",
    "children": [],
    "dataSet": []
  },
  {
    "id": 25,
    "children": [],
    "dataSet": []
  },
  {
    "id": "BE",
    "children": [],
    "dataSet": []
  },
  {
    "id": "china",
    "children": [],
    "dataSet": []
  },
  {
    "id": 23,
    "children": [],
    "dataSet": []
  },
  {
    "id": "MA",
    "children": [],
    "dataSet": []
  },
  {
    "id": "india",
    "children": [],
    "dataSet": []
  },
  {
    "id": 23,
    "children": [],
    "dataSet": []
  },
  {
    "id": "BE",
    "children": [],
    "dataSet": []
  },
  {
    "id": "india",
    "children": [],
    "dataSet": []
  },
  {
    "id": 23,
    "children": [],
    "dataSet": []
  },
  {
    "id": "MBA",
    "children": [],
    "dataSet": []
  }
]

Upvotes: 1

Views: 119

Answers (2)

Ajai
Ajai

Reputation: 391

var aa = [{"id":1,"name":"ajai","country":"india","qualification":"BE","age":23},{"id":2,"name":"aravindh","country":"india","qualification":"BCOM","age":24},{"id":3,"name":"gopal","country":"china","qualification":"MA","age":23},{"id":4,"name":"guvaliour","country":"china","qualification":"BE","age":24},{"id":5,"name":"anavarthan","country":"japan","qualification":"BE","age":25},{"id":6,"name":"veer","country":"china","qualification":"MA","age":23},{"id":7,"name":"ramani","country":"india","qualification":"BE","age":23},{"id":8,"name":"kumar","country":"india","qualification":"MBA","age":23}];

  var bb=[];
    var cc = ['country','age','qualification'];
    for(var i=0;i<=aa.length-1;i++){
      cc.reduce((acc,ele,index)=>{
        let obj={
          id: ele,
          name: aa[i][ele],
          children:[],
          dataSet: []
        }
        let pos = acc.map(ele=>ele.name).indexOf(aa[i][ele]);
        let ind = 0;
        if(pos == -1){
         acc.push(obj);
         ind = acc.length-1 < 0 ? 0 : acc.length-1;
        }else{
         ind=pos;
        }
        return acc[ind]['children'];
      },bb)
    }
    console.log(bb)

Upvotes: 1

trincot
trincot

Reputation: 350147

You could use a nested Map to create the tree structure, and then iterate that tree recursively to translate those maps to the plain, nested array of objects:

function transform(data, props) {
    // A recursive function to translate nested maps to plain objects/arrays
    const recur = (map, depth) => Array.from(map, ([id, value]) => depth ? {
        id,
        children: recur(value, depth - 1)
    } : {
        id,
        children: [],
        dataSet: Array.from(value.values())
    });

    // First create a nested Map to efficiently create the tree
    let result = new Map;
    for (let obj of data) {
        let map = result;
        for (let prop of props) {
            let key = obj[prop];
            let children = map.get(key);
            if (!children) map.set(key, children = new Map);
            map = children;
        }
        map.set(map.size, obj); // Deepest level is array-like
    }
    // Use recursion to convert the nested Map to the desired object structure
    return recur(result, props.length - 1);
}

// Demo on sample data
var aa = [{"id":1,"name":"ajai","country":"india","qualification":"BE","age":23},{"id":2,"name":"aravindh","country":"india","qualification":"BCOM","age":24},{"id":3,"name":"gopal","country":"china","qualification":"MA","age":23},{"id":4,"name":"guvaliour","country":"china","qualification":"BE","age":24},{"id":5,"name":"anavarthan","country":"japan","qualification":"BE","age":25},{"id":6,"name":"veer","country":"china","qualification":"MA","age":23},{"id":7,"name":"ramani","country":"india","qualification":"BE","age":23},{"id":8,"name":"kumar","country":"india","qualification":"MBA","age":23}];

var bb = ['country','age','qualification']

console.log(transform(aa, bb));
 

Upvotes: 1

Related Questions