简体   繁体   English

将具有不同长度的深层嵌套JSON对象重新格式化为简化对象数组

[英]Reformatting deeply nested JSON object with varying length into an array of simplified objects

I'm using a map-reduce function to merge multiple data inputs into a single object as mentioned here . 我使用的map-reduce功能提到的多个数据输入合并成一个单一的对象这里

The received reduced Object is in the following format: 收到的缩减对象采用以下格式:

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

However I'm looking for a way to reformat it into an array of objects which will allow to iterate through the objects and access all the data easily : 但是,我正在寻找一种方法将其重新格式化为一个对象数组,这些对象将允许迭代对象并轻松访问所有数据:

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

You could take a dynamic approach by handing over an array of nested keys and take all keys for new objects. 您可以通过移交一组嵌套键并采用所有键来获取新对象来采用动态方法。

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

 const obj = { "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" ] } } } } }; const getObjecta = (object, val) => { const a = {}, b = {}; a.Date = val; b.Date = val; a.StartTimeLocalized = Object.keys(object)[0]; b.StartTimeLocalized = Object.keys(object)[0]; a.EndTimeLocalized = Object.keys(object[a.StartTimeLocalized])[0]; b.EndTimeLocalized = Object.keys(object[b.StartTimeLocalized])[0]; a.LabelWithCompany = '[MET] L2 - NB'; b.LabelWithCompany = '[MET] L2 - CS'; a.attendees = object[a.StartTimeLocalized][a.EndTimeLocalized]['[MET] L2 - NB']['attendees']; b.attendees = object[b.StartTimeLocalized][b.EndTimeLocalized]['[MET] L2 - CS']['attendees']; return [a, b]; }; let finalArray = []; Object.keys(obj).forEach((val) => { finalArray = [...finalArray, ...getObjecta(obj[val], val)]; }); console.log(finalArray); 

You can have 4 levels of nested loops and push the result into an array. 您可以拥有4级嵌套循环并将结果推送到数组中。 To map over an object you can make use of Object.entries to get key-value pairs at each level as an array 要映射对象,可以使用Object.entries将每个级别的键值对作为数组获取

 var obj = { "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" ] } } } } } const res = []; Object.entries(obj).forEach(([date, value]) => { Object.entries(value).forEach(([start, value2]) => { Object.entries(value2).forEach(([end, value3]) => { Object.entries(value3).forEach(([label, value4]) => { res.push({ Date: date, StartTimeLocalized: start, EndTimeLocalized: end, LabelWithCompany: label, attendees: value4.attendees }) }) }) }) }) console.log(res) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM