Reputation: 778
I have the following JSON object that I need to un-nest, regroup and re-nest by the state. I understand that there must be some kind of for-loop and group by operation that needs to be performed.
[
{
"Date": "2000-01-01T08:00:00.000Z",
"Florida": "4626",
"New York": "210",
"Pennsylvania": "1500",
"Virginia": "9",
"West Virginia": "1400",
"Illinois": "12206",
"Indiana": "2098",
"Kansas": "34463",
"Kentucky": "3465",
"Michigan": "7907",
"Missouri": "94",
"Nebraska": "2957",
"North Dakota": "32719",
"Ohio": "6575",
"Oklahoma": "69976",
"South Dakota": "1170",
"Tennessee": "346",
"Alabama": "10457",
"Arkansas": "7154",
"Louisiana": "105425",
"Mississippi": "19844",
"New Mexico": "67198",
"Texas": "443397",
"Colorado": "18481",
"Montana": "15428",
"Utah": "15636",
"Wyoming": "60726",
"Alaska": "355199",
"Alaska South": "10590",
"Arizona": "59",
"California": "271132",
"Nevada": "621",
"": ""
},
{
"Date": "2001-01-01T08:00:00.000Z",
"Florida": "4426",
"New York": "166",
"Pennsylvania": "1620",
"Virginia": "11",
"West Virginia": "1226",
"Illinois": "10092",
"Indiana": "2022",
"Kansas": "33942",
"Kentucky": "2969",
"Michigan": "7375",
"Missouri": "91",
"Nebraska": "2922",
"North Dakota": "31691",
"Ohio": "6051",
"Oklahoma": "68531",
"South Dakota": "1255",
"Tennessee": "351",
"Alabama": "9334",
"Arkansas": "7592",
"Louisiana": "104610",
"Mississippi": "19528",
"New Mexico": "68001",
"Texas": "424297",
"Colorado": "16520",
"Montana": "15920",
"Utah": "15252",
"Wyoming": "57433",
"Alaska": "351411",
"Alaska South": "11500",
"Arizona": "59",
"California": "260663",
"Nevada": "572",
"": ""
},
{
"Date": "2002-01-01T08:00:00.000Z",
"Florida": "3634",
"New York": "164",
"Pennsylvania": "2324",
"Virginia": "25",
"West Virginia": "1456",
"Illinois": "11100",
"Indiana": "1962",
"Kansas": "33380",
"Kentucky": "2721",
"Michigan": "7218",
"Missouri": "95",
"Nebraska": "2782",
"North Dakota": "30803",
"Ohio": "5631",
"Oklahoma": "66421",
"South Dakota": "1214",
"Tennessee": "275",
"Alabama": "8636",
"Arkansas": "7252",
"Louisiana": "93321",
"Mississippi": "19371",
"New Mexico": "67562",
"Texas": "405776",
"Colorado": "20522",
"Montana": "16990",
"Utah": "13771",
"Wyoming": "54801",
"Alaska": "359382",
"Alaska South": "11303",
"Arizona": "63",
"California": "257898",
"Nevada": "553",
"": ""
}
]
I would like to process the above into the following format where state key is state name and production key is an array of dictionaries with years and production for that year.
[
{
"state": "California",
"production": [
{
"Date": "2000-01-01T08:00:00.000Z",
"production": 1000
},
{
"Date": "2001-01-01T08:00:00.000Z",
"production": 2000
}
]
},
{
"state": "New York",
"production": [
{
"Date": "2000-01-01T08:00:00.000Z",
"production": 4000
},
{
"Date": "2001-01-01T08:00:00.000Z",
"production": 5000
}
]
}
]
Please let me know what operations are needed to perform to achive the above format. Thank you!
Upvotes: 0
Views: 59
Reputation: 386550
Maybe this is working for you, with some iteration and an object as reference.
var data = [{ "Date": "2000-01-01T08:00:00.000Z", "Florida": "4626", "New York": "210", "Pennsylvania": "1500", "Virginia": "9", "West Virginia": "1400", "Illinois": "12206", "Indiana": "2098", "Kansas": "34463", "Kentucky": "3465", "Michigan": "7907", "Missouri": "94", "Nebraska": "2957", "North Dakota": "32719", "Ohio": "6575", "Oklahoma": "69976", "South Dakota": "1170", "Tennessee": "346", "Alabama": "10457", "Arkansas": "7154", "Louisiana": "105425", "Mississippi": "19844", "New Mexico": "67198", "Texas": "443397", "Colorado": "18481", "Montana": "15428", "Utah": "15636", "Wyoming": "60726", "Alaska": "355199", "Alaska South": "10590", "Arizona": "59", "California": "271132", "Nevada": "621", "": "" }, { "Date": "2001-01-01T08:00:00.000Z", "Florida": "4426", "New York": "166", "Pennsylvania": "1620", "Virginia": "11", "West Virginia": "1226", "Illinois": "10092", "Indiana": "2022", "Kansas": "33942", "Kentucky": "2969", "Michigan": "7375", "Missouri": "91", "Nebraska": "2922", "North Dakota": "31691", "Ohio": "6051", "Oklahoma": "68531", "South Dakota": "1255", "Tennessee": "351", "Alabama": "9334", "Arkansas": "7592", "Louisiana": "104610", "Mississippi": "19528", "New Mexico": "68001", "Texas": "424297", "Colorado": "16520", "Montana": "15920", "Utah": "15252", "Wyoming": "57433", "Alaska": "351411", "Alaska South": "11500", "Arizona": "59", "California": "260663", "Nevada": "572", "": "" }, { "Date": "2002-01-01T08:00:00.000Z", "Florida": "3634", "New York": "164", "Pennsylvania": "2324", "Virginia": "25", "West Virginia": "1456", "Illinois": "11100", "Indiana": "1962", "Kansas": "33380", "Kentucky": "2721", "Michigan": "7218", "Missouri": "95", "Nebraska": "2782", "North Dakota": "30803", "Ohio": "5631", "Oklahoma": "66421", "South Dakota": "1214", "Tennessee": "275", "Alabama": "8636", "Arkansas": "7252", "Louisiana": "93321", "Mississippi": "19371", "New Mexico": "67562", "Texas": "405776", "Colorado": "20522", "Montana": "16990", "Utah": "13771", "Wyoming": "54801", "Alaska": "359382", "Alaska South": "11303", "Arizona": "63", "California": "257898", "Nevada": "553", "": "" }],
grouped = [];
data.forEach(function (a) {
Object.keys(a).forEach(function (k) {
if (k !== 'Date') {
if (!this[k]) {
this[k] = { state: k, production: [] };
grouped.push(this[k]);
}
this[k].production.push({ Date: a.Date, production: a[k] });
}
}, this);
}, Object.create(null));
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');
Upvotes: 3