简体   繁体   中英

Split a JSON file into two and create a two dimensional array in Javascript?

I have the following JSON object:

var data = [
    {
        "re_ID": "15.02",
        "date": "20.01.2016",
        "name": "Opening",
        "booking_ID": "4343",
        "source": "int",
        "meaning": "high",
        "date_booking": "31.05.2018"
    },
    {
        "re_ID": "15.02",
        "date": "20.01.2016",
        "name": "Opening",
        "booking_ID": "333",
        "source": "int",
        "meaning": "high",
        "date_booking": "31.04.2018"
    },
    {
        "re_ID": "15.02",
        "date": "20.01.2016",
        "name": "Opening",
        "booking_ID": "434311",
        "source": "int",
        "meaning": "high",
        "date_booking": "31.01.2018"
    },
    {
        "re_ID": "99.33",
        "date": "11.08.2013",
        "name": "medium",
        "booking_ID": "5411",
        "source": "ext",
        "meaning": "low",
        "date_booking": "11.11.2017"
    }
]

I need to transform this into a nested format like in the following example such that I can loop through it with a double loop to get the data from the first dimension while with the second loop I can get the information of the second dimension of it:

The structure I need to have:

var data = [
    {
        "re_ID": "15.02",
        "date": "20.01.2016",
        "name": "Opening"
    },
    {
        "booking_ID": "4343",
        "source": "int",
        "meaning": "high",
        "date_booking": "31.05.2018"
    },
    {
        "booking_ID": "333",
        "source": "int",
        "meaning": "high",
        "date_booking": "31.04.2018"
    },
    {
        "booking_ID": "434311",
        "source": "int",
        "meaning": "high",
        "date_booking": "31.01.2018"
    },
    {
        "re_ID": "99.33",
        "date": "11.08.2013",
        "name": "medium"
    },
    {
        "booking_ID": "5411",
        "source": "ext",
        "meaning": "low",
        "date_booking": "11.11.2017"
    }
];

How can I build this second JSON from the first JSON

Well, I assume the structure you say you want output is not actually what you want, but rather to put the sub objects into an array instead.

I'm also assuming that if the re_ID is the same, then date and name will be the same as well.

You just need to loop through the data array and then group the transformed subobjects by the re_ID. When you're done, transform the collector object back into an array:

(Note that if you need to support IE, you'll need a polyfill for Array.includes())

function transformJSON(data) {
    var collector = {};
    var ids = [];

    for (var i = 0; i < data.length; i++) {
        var reid = data[i].re_ID;
        if (!ids.includes(reid)) {
            collector[reid] = {
                re_ID: reid,
                date: data[i].date,
                name: data[i].name,
                items: []
            };

            ids.push(reid);
        }

        collector[reid].items.push({
            booking_ID: data[i].booking_ID,
            source: data[i].source,
            meaning: data[i].meaning,
            date_booking: data[i].date_booking
        });
    }

    var output = [];
    for (var i = 0; i < ids.length; i++) {
        output.push(collector[ids[i]]);
    }

    return output;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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