lodash groupby键(如果集合中存在) [英] lodash groupby key if exist in collection

查看:442
本文介绍了lodash groupby键(如果集合中存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数组下面

{
  "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"
}

并且输出应如下所示

{
"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,

在lodash或任何角度都可以实现此功能.

In lodash or anything angular does this functionality.

我一无所知, 我只给出下面的代码

I am clueless to start, I just give below code

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

但是输出不符合预期.

请帮助或指导

谢谢

推荐答案

您需要一种不同的方法,而不是分组,而是从相关数据中创建树.

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

此解决方案使用以id作为键并且也使用parentID的数组.由于存储了子代与父代以及父代与子代之间的关系,因此该代码可用于单个循环.

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:

基本上,对于数组中的每个对象,构建新对象所用的id都与新对象中的parentID一样.

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.

例如,这个对象

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

首先使用id此属性在o中生成

it generates in o first with id this property

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

,然后使用parentID

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

当所有对象都像这样处理时,我们终于得到了一棵树.

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

最后,返回root属性的children数组.

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; }

这篇关于lodash groupby键(如果集合中存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆