我有以下JSON对象,我需要通过状态取消嵌套,重新组合和重新嵌套。我知道必须有某种for循环和分组操作需要执行。
[
{
"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",
"": ""
}
]
我想将上面的内容处理成以下格式:状态键是州名,生产键是一个包含年份和生产的字典数组。
[
{
"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
}
]
}
]
请让我知道执行上述格式需要执行哪些操作。谢谢!
答案 0 :(得分:3)
也许这对你有用,有一些迭代和一个对象作为参考。
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>');