Mahir Altınkaya
Mahir Altınkaya

Reputation: 439

Change Array to Object with recursives

i have an array shared below. I need to change this array like an object. I tried something but cant make it. I Want to make like this with foreach or recursive functions.

let nodes= [
  { id: 1, name: "Denny Curtis", title: "CEO", img: "https://cdn.balkan.app/shared/2.jpg" },
  { id: 2, pid: 1, name: "Ashley Barnett", title: "Sales Manager", img: "https://cdn.balkan.app/shared/3.jpg" },
  { id: 3, pid: 1, name: "Caden Ellison", title: "Dev Manager", img: "https://cdn.balkan.app/shared/4.jpg" }
];

let newObj =     {
  "id": 1,
  "name": "Denny Curtis",
  "title": "CEO",
  "img": "https://cdn.balkan.app/shared/2.jpg",
  "children": [
    {
      "id": 2,
      "pid": 1,
      "name": "Ashley Barnett",
      "title": "Sales Manager",
      "img": "https://cdn.balkan.app/shared/3.jpg",
      "children": [
        {
          "id": 4,
          "pid": 2,
          "name": "Elliot Patel",
          "title": "Sales",
          "img": "https://cdn.balkan.app/shared/5.jpg"
        },
        {
          "id": 5,
          "pid": 2,
          "name": "Lynn Hussain",
          "title": "Sales",
          "img": "https://cdn.balkan.app/shared/6.jpg"
        }
      ]
    }
  ]
}

Thank you from now.

Upvotes: 0

Views: 22

Answers (1)

Logan Sotelo
Logan Sotelo

Reputation: 32

It would probably be better to make an array of users with the children related to them.

let nodes = [{
        id: 1,
        name: "Denny Curtis",
        title: "CEO",
        img: "https://cdn.balkan.app/shared/2.jpg"
    },
    {
        id: 2,
        pid: 1,
        name: "Ashley Barnett",
        title: "Sales Manager",
        img: "https://cdn.balkan.app/shared/3.jpg"
    },
    {
        id: 3,
        pid: 1,
        name: "Caden Ellison",
        title: "Dev Manager",
        img: "https://cdn.balkan.app/shared/4.jpg"
    }
];

let newNodeArray = [];

nodes.forEach(node=>{
  newNodeArray.push({...node,children: nodes.filter(filterNode => filterNode.pid === node.id )});
});

console.log(JSON.stringify(newNodeArray)); 

The outcome of this one would look like:

[
   {
      "id":1,
      "name":"Denny Curtis",
      "title":"CEO",
      "img":"https://cdn.balkan.app/shared/2.jpg",
      "children":[
         {
            "id":2,
            "pid":1,
            "name":"Ashley Barnett",
            "title":"Sales Manager",
            "img":"https://cdn.balkan.app/shared/3.jpg"
         },
         {
            "id":3,
            "pid":1,
            "name":"Caden Ellison",
            "title":"Dev Manager",
            "img":"https://cdn.balkan.app/shared/4.jpg"
         }
      ]
   },
   {
      "id":2,
      "pid":1,
      "name":"Ashley Barnett",
      "title":"Sales Manager",
      "img":"https://cdn.balkan.app/shared/3.jpg",
      "children":[]
   },
   {
      "id":3,
      "pid":1,
      "name":"Caden Ellison",
      "title":"Dev Manager",
      "img":"https://cdn.balkan.app/shared/4.jpg",
      "children":[]
   }
]
//This is how you would do it if you want the response to be an object

let nodeObject = {...nodes.filter(node=>!node.pid)[0],children:[]};
nodeObject.children = nodes.filter(node=>nodeObject.id === node.pid);
nodeObject.children = nodeObject.children.map(node=>({...node,children: nodes.filter(filterNode=>node.id === filterNode.pid)}));
console.log(nodeObject); 

// The Response for this would be:

{
    "id": 1,
    "name": "Denny Curtis",
    "title": "CEO",
    "img": "https://cdn.balkan.app/shared/2.jpg",
    "children": [{
        "id": 2,
        "pid": 1,
        "name": "Ashley Barnett",
        "title": "Sales Manager",
        "img": "https://cdn.balkan.app/shared/3.jpg",
        "children": [{
            "id": 4,
            "pid": 2,
            "name": "Caden Ellison",
            "title": "Sales",
            "img": "https://cdn.balkan.app/shared/4.jpg"
        }]
    }, {
        "id": 3,
        "pid": 1,
        "name": "Caden Ellison",
        "title": "Dev Manager",
        "img": "https://cdn.balkan.app/shared/4.jpg",
        "children": []
    }]
}

Upvotes: 1

Related Questions