编程语言
首页 > 编程语言> > javascript-将深度可变的深度嵌套的JSON对象重新格式化为简化对象的数组

javascript-将深度可变的深度嵌套的JSON对象重新格式化为简化对象的数组

作者:互联网

我正在使用map-reduce函数将多个数据输入合并到单个对象中,如here所述.

收到的精简对象的格式如下:

{
    "2019-04-02T00:00:00-04:00": {
        "2019-04-02T09:00:00-04:00": {
            "2019-04-02T18:00:00-04:00": {
                "[MET] L2 - NB": {
                    "attendees": [
                        "Lex Luthor",
                        "Lois Lane"
                    ]
                },
                "[MET] L2 -  CS": {
                    "attendees": [
                        "General Zod",
                        "Clark Kent"
                    ]
                }
            }
        }
    },
    "2019-04-03T00:00:00-04:00": {
        "2019-04-03T09:00:00-04:00": {
            "2019-04-03T18:00:00-04:00": {
                "[MET] L2 - NB": {
                    "attendees": [
                        "Lex Luthor",
                        "Lois Lane"
                    ]
                },
                "[MET] L2 -  CS": {
                    "attendees": [
                        "General Zod",
                        "Clark Kent"
                    ]
                }
            }
        }
    }
}

但是,我正在寻找一种将其重新格式化为对象数组的方法,该方法将允许迭代对象并轻松访问所有数据:

[
    {
        Date: "2019-04-02T00:00:00-04:00",
        StartTimeLocalized: "2019-04-02T09:00:00-04:00",
        EndTimeLocalized: "2019-04-02T18:00:00-04:00",
        LabelWithCompany: "[MET] L2 - NB",
        attendees: [
            "Lex Luthor",
            "Lois Lane"
        ]
    }, {
        Date: "2019-04-02T00:00:00-04:00",
        StartTimeLocalized: "2019-04-02T09:00:00-04:00",
        EndTimeLocalized: "2019-04-02T18:00:00-04:00",
        LabelWithCompany: "[MET] L2 -  CS",
        attendees: [
            "General Zod",
            "Clark Kent"
        ]
    },
    {
        Date: "2019-04-03T00:00:00-04:00",
        StartTimeLocalized: "2019-04-03T09:00:00-04:00",
        EndTimeLocalized: "2019-04-03T18:00:00-04:00",
        LabelWithCompany: "[MET] L2 -  CS",
        attendees: [
            "Lex Luthor",
            "Lois Lane"
        ]
    },
    {
        Date: "2019-04-03T00:00:00-04:00",
        StartTimeLocalized: "2019-04-03T09:00:00-04:00",
        EndTimeLocalized: "2019-04-03T18:00:00-04:00",
        LabelWithCompany: "[MET] L2 -  CS",
        attendees: [
            "General Zod",
            "Clark Kent"
        ]
    }
]

解决方法:

您可以通过传递一组嵌套键并采用所有新对象键来采用动态方法.

function mapNestedObjects(object, keys) {
    function getNested(source, target, index) {
        Object.entries(source).forEach(([key, value]) => {
            if (index + 1 < keys.length) {
                getNested(value, { ...target, [keys[index]]: key }, index + 1);
            } else {
                result.push({ ...target, [keys[index]]: key, ...value });
            }
        });
    }

    var result = [];
    getNested(object, {}, 0);
    return result;
}

var object = { "2019-04-02T00:00:00-04:00": { "2019-04-02T09:00:00-04:00": { "2019-04-02T18:00:00-04:00": { "[MET] L2 - NB": { attendees: ["Lex Luthor", "Lois Lane"] }, "[MET] L2 -  CS": { attendees: ["General Zod", "Clark Kent"] } } } }, "2019-04-03T00:00:00-04:00": { "2019-04-03T09:00:00-04:00": { "2019-04-03T18:00:00-04:00": { "[MET] L2 - NB": { attendees: ["Lex Luthor", "Lois Lane"] }, "[MET] L2 -  CS": { attendees: ["General Zod", "Clark Kent"] } } } } },
    keys = ["Date", "StartTimeLocalized", "EndTimeLocalized", "LabelWithCompany"],
    result = mapNestedObjects(object, keys);

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

标签:javascript-objects,node-js,javascript
来源: https://codeday.me/bug/20191108/2005936.html