JS通过深度嵌套的对象对JSON进行归类和分组 [英] JS Reduce and group JSON by deeply nested object

查看:101
本文介绍了JS通过深度嵌套的对象对JSON进行归类和分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过REST提取一个JSON,其中包含带有一些字段和一些嵌套对象的对象数组.

I'm pulling via REST a JSON with an array of objects with some fields and some nested objects.

我要创建的是嵌套JSON对象数组中的分组摘要对象,其结构如下:

What I'm trying to create is a grouped summary object from the array of nested JSON objects with the following structure:

 var data = [
        {
            "Id": 79,
            "Date": "2019-02-17T00:00:00-07:00",
            "StartTime": 1535385600,
            "EndTime": 1535416200,
            "Slots": [
                {
                    "blnEmptySlot": false,
                    "strType": "B",
                    "intStart": 3600,
                    "intEnd": 5400,
                    "intUnixStart": 1535389200,
                    "intUnixEnd": 1535391000,
                }
            ],
            "OperationalUnit": 3,
            "Created": "2019-01-31T11:23:29+02:00",
            "StartTimeLocalized": "2019-02-17T10:00:00+02:00",
            "EndTimeLocalized": "2019-02-17T19:00:00+02:00",
            "_MetaData": {
                "AttendeeInfo": {
                    "Id": 111,
                    "AttendeeDisplayName": "att_name1",
                    "AttendeeProfile": 406,
                    "Attendee": 406,
                    "Photo": "avatar_path"
                  },
                "OperationalUnitInfo": {
                    "Id": 3,
                    "OperationalUnitName": "op_unit_name",
                    "Company": 1,
                    "CompanyName": "comp_name",
                    "LabelWithCompany": "comp_label"
                },
            }
        },
        {
            "Id": 80,
            "Date": "2019-02-17T00:00:00-07:00",
            "StartTime": 1535385600,
            "EndTime": 1535416200,
            "Slots": [
                {
                    "blnEmptySlot": false,
                    "strType": "B",
                    "intStart": 3600,
                    "intEnd": 5400,
                    "intUnixStart": 1535389200,
                    "intUnixEnd": 1535391000,
                }
            ],
            "OperationalUnit": 3,
            "Created": "2019-01-31T11:23:29+02:00",
            "StartTimeLocalized": "2019-02-17T10:00:00+02:00",
            "EndTimeLocalized": "2019-02-17T19:00:00+02:00",
            "_MetaData": {
                "AttendeeInfo": {
                    "Id": 112,
                    "AttendeeDisplayName": "att_name2",
                    "AttendeeProfile": 407,
                    "Attendee": 407,
                    "Photo": "avatar_path"
                  },
                "OperationalUnitInfo": {
                    "Id": 3,
                    "OperationalUnitName": "op_unit_name",
                    "Company": 1,
                    "CompanyName": "comp_name",
                    "LabelWithCompany": "comp_label"
                },
            }
        }
    ];

总体目的是创建一个具有开始和结束日期时间的事件对象列表,一个LabelWithCompany字符串(深层嵌套对象)以及一个按OperationalUnitName以及开始时间和结束时间分组的所有与会者的列表.

The overall purpose is to create a list of event objects with start and end DateTime, a LabelWithCompany String (deep nested object) and a list of all Attendees grouped by OperationalUnitName and Start and End times.

示例:

[
    {
        "2019-02-17T00:00:00-08:00": {//Date
            "2019-02-17T10:00:00+02:00": {//StartTimeLocalized
                "2019-02-17T19:00:00+02:00": {//EndTimeLocalized
                    "[NYC] Network - Solutions": {//LabelWithCompany
                        "attendees": [
                            "att_name1",//AttendeeDisplayName
                            "att_name2"  //AttendeeDisplayName    
                        ]
                    }
                }
            }
        }
    }
]

我认为map和reduce是必需的功能,但是在执行多个嵌套的reduce时遇到了麻烦. 关于如何将这些对象聚合为包含每个会话的所有与会者的聚合格式的任何其他建议.

I assume that map and reduce are the necessary functions, but I had trouble with doing multiple nested reduces. Any other suggestion how to aggregate these objects to into an aggregated format that will contain all the attendees for each session.

推荐答案

您可以使用一些辅助函数来获取嵌套对象的值,并通过获取键数组来设置路径并返回最后一个嵌套对象.

You could take some helper function for getting the value of a nested object and for setting a path and returning the last nested object by taking an array of keys.

然后,您需要获取嵌套结果集的键,而另一个键则需要获取推入结果的值.

Then you need to take the keys for the nested result set an another for getting the value for pushing to the result.

const
    getValue = (object, path) => [].concat(path).reduce((o, k) => o[k], object),
    setPath = (object, path) => [].concat(path).reduce((o, k) => o[k] = o[k] || {}, object);

var data = [{ Id: 79, Date: "2018-08-27T00:00:00-07:00", StartTime: 1535385600, EndTime: 1535416200, Slots: [{ blnEmptySlot: false, strType: "B", intStart: 3600, intEnd: 5400, intUnixStart: 1535389200, intUnixEnd: 1535391000 }], OperationalUnit: 3, Created: "2019-01-31T11:23:29+02:00", StartTimeLocalized: "2019-02-17T10:00:00+02:00", EndTimeLocalized: "2019-02-17T19:00:00+02:00", _MetaData: { AttendeeInfo: { Id: 111, AttendeeDisplayName: "att_name1", AttendeeProfile: 406, Attendee: 406, Photo: "avatar_path" }, OperationalUnitInfo: { Id: 3, OperationalUnitName: "op_unit_name", Company: 1, CompanyName: "comp_name", LabelWithCompany: "comp_label" } } }, { Id: 80, Date: "2018-08-27T00:00:00-07:00", StartTime: 1535385600, EndTime: 1535416200, Slots: [{ blnEmptySlot: false, strType: "B", intStart: 3600, intEnd: 5400, intUnixStart: 1535389200, intUnixEnd: 1535391000 }], OperationalUnit: 3, Created: "2019-01-31T11:23:29+02:00", StartTimeLocalized: "2019-02-17T10:00:00+02:00", EndTimeLocalized: "2019-02-17T19:00:00+02:00", _MetaData: { AttendeeInfo: { Id: 112, AttendeeDisplayName: "att_name2", AttendeeProfile: 407, Attendee: 407, Photo: "avatar_path" }, OperationalUnitInfo: { Id: 3, OperationalUnitName: "op_unit_name", Company: 1, CompanyName: "comp_name", LabelWithCompany: "comp_label" } } }],
    keys = ["Date", "StartTimeLocalized", "EndTimeLocalized", ["_MetaData", "OperationalUnitInfo", "LabelWithCompany"]],
    value = ["_MetaData", "AttendeeInfo", "AttendeeDisplayName"],
    result = data.reduce((r, o) => {
        var temp = setPath(r, keys.map(getValue.bind(null, o)));

        temp.attendees = temp.attendees || [];
        temp.attendees.push(getValue(o, value));
        return r;
    }, {});

console.log(result);

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

这篇关于JS通过深度嵌套的对象对JSON进行归类和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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