我有一个想要标准化的平面数据集。数据集是存在的,源是不可更改的,因此定义更好的数据结构不是一种选择。我已经设法“规范化”并重组数据,但我不确定当行数开始增长时它的效率如何?这就是我所拥有的,就像我说的那样,它只是在寻找更优雅的解决方案......
这是理想输出的样子
{ArticleID: "ABC123",
Status: "Scheduled",
Articles: {
ArticleID: "ABC123",
Region: US,
PubTypeID: "EO",
PubType: "Europe Order",
Created: "01/01/2013",
Scheduled: "03/02/2013"
},{
ArticleID: "ABC234",
Region: US,
PubTypeID: "EO",
PubType: "Europe Order",
Created: "01/01/2013",
Scheduled: "03/02/2013"
}
}
$(document).ready(function(){
var data = new Array(
{
ArticleID: "ABC123",
Region: 'US',
PubTypeID: "EO",
PubType: "Europe Order",
Created: "01/01/2013",
Scheduled: "03/02/2013"
},
{
ArticleID: "ABC123",
Region: 'RU',
PubTypeID: "EO",
PubType: "Europe Order",
Created: "01/01/2013",
Scheduled: "04/02/2013"
},
{
ArticleID: "ABC763",
Region: 'EO',
PubTypeID: "EO",
PubType: "Europe Order",
Created: "01/01/2013",
Scheduled: "03/02/2013"
},
{
ArticleID: "ABC498",
Region: 'JP',
PubTypeID: "EN",
PubType: "Europe Order",
Created: "01/01/2013",
Scheduled: "03/02/2013"
}
);
var Articles = new Array();
var pID = "";
var cID = "";
$.each(data, function(i, item) {
cID = item.ArticleID;
var entries = new Array();
if(cID != pID)
{
$.each(data, function(j, item2) {
var entry = new Object();
if(cID === item2.ArticleID)
{
entry.ArticlID = item2.ArticleID;
entry.Region = item2.Region;
entry.PubTypeID = item2.PubTypeID
entries.push(entry);
}
});
Articles.push({ArtilceID: cID, Entries: entries})
pID = cID
}
});
alert(Articles.length);
alert(Articles[0].Entries.length);
});