lodash groupby key if exist in collection

2019-10-03 16:13发布

问题:

I have below an array

{
  "sec": "11",
  "details": [
    {
      "id": "1",
      "user": "Me1"
    },
    {
      "id": "2",
      "uesr": "Me2"
    },
    {
      "id": "3",
      "user": "Me3"
    }
    {
      "id": "4",
      "user": "Me4",
      parentID:"2"
    },
    {
      "id": "5",
      "uesr": "Me5"
    },
    {
      "id": "6",
      "user": "Me6",
      parentID:"2"
    }
    {
      "id": "7",
      "user": "Me7"
    },
    {
      "id": "8",
      "uesr": "Me8",
      parentID:"7"
    },
    {
      "id": "9",
      "user": "Me9",
      parentID:"7"
    }
  ],
  "isDisplay": "true"
}

and output should be like below

{
"sec":"11",
"details":[
{
"id":"1",
"user":"Me1"
},
{
"id":"2",
"uesr":"Me2",
"childs":[
{
"id":"4",
"user":"Me4",
"parentID":"2"
},
{
"id":"6",
"user":"Me6",
"parentID":"2"
}
]
},
{
"id":"3",
"user":"Me3"
},
{
"id":"5",
"uesr":"Me5"
},
{
"id":"7",
"user":"Me7",
"childs":[
{
"id":"8",
"uesr":"Me8",
"parentID":"7"
},
{
"id":"9",
"user":"Me9",
"parentID":"7"
}
]
}
],
"isDisplay":"true"
}

I can do this by simple looping,

In lodash or anything angular does this functionality.

I am clueless to start, I just give below code

this.list = _.groupBy(this.list,"parentID");

But the output not as expected.

Please help or guide

Thanks

回答1:

You need a different approach, not grouping, but creating a tree out of the related data.

This solution uses an array with id as key and with parentID as well. The code works with a single loop, because of storing of the relation of children and parent and parent to their children.

How it works:

Basically for every object in the array it takes as well the id for building a new object as the parentID for a new object.

So for example this object

{ id: "6", parentID: "2", user: "Me6" }

it generates in o first with id this property

6: {
    id: "6",
    parentID: "2",
    user: "Me6"
}

and then this property with parentID

2: {
    children: [
        {
            id: "6",
            parentID: "2",
            user: "Me6"
        }
    ]
},

and while all object treated like this, we finally get a tree.

At the end, the children array of the root property is returned.

function getTree(data, root) {
    var o = {};
    data.forEach(function (a) {
        if (o[a.id] && o[a.id].children) {
            a.children = o[a.id].children;
        }
        o[a.id] = a;
        o[a.parentID] = o[a.parentID] || {};
        o[a.parentID].children = o[a.parentID].children || [];
        o[a.parentID].children.push(a);
    });
    return o[root].children;
}

var data = { sec: "11", details: [{ id: "1", user: "Me1" }, { id: "2", uesr: "Me2" }, { id: "3", user: "Me3" }, { id: "4", user: "Me4", parentID: "2" }, { id: "5", uesr: "Me5" }, { id: "6", user: "Me6", parentID: "2" }, { id: "7", user: "Me7" }, { id: "8", user: "Me8", parentID: "7" }, { id: "9", user: "Me9", parentID: "7" }], isDisplay: "true" },
    result = { sec: "11", details: getTree(data.details, undefined), isDisplay: "true" };

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