我有一个json数组
[
{Age: "01-10 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []}
,{Age: "01-10 Days", EventName: "Invoice Distribution", Value: 5, ActiveInvoices: []}
,{Age: "01-10 Days", EventName: "Invoice Pay Pending", Value: 2, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Approval", Value: 2, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Distribution", Value: 11, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Paid", Value: 1, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Pay Pending", Value: 2, ActiveInvoices: []}
]
请帮我在javascript中将其排序到下面的数组中。 EventName字段应分别为01-10天和10-20天等Age字段。我制作并尝试了这个jsfiddle ..请检查。 http://jsfiddle.net/h829p07n/1/
// Required Output
[
{Age: "01-10 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []}
,{Age: "01-10 Days", EventName: "Invoice Distribution", Value: 5, ActiveInvoices: []}
,{Age: "01-10 Days", EventName: "Invoice Pay Pending", Value: 2, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Distribution", Value: 11, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Pay Pending", Value: 2, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Approval", Value: 2, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Paid", Value: 1, ActiveInvoices: []}
]
提前致谢
答案 0 :(得分:0)
你在最后一个for循环中的代码中有一些错误(var count未正确使用) 在这里看一个演示http://jsfiddle.net/h829p07n/7/
for(var i = 0 ; i < tempArra.length-1 ; i++){
count = tempArra[i].length;
for(var k = 0; k < count ; k++){
for(var L = 0; L < count; L++){
if(tempArra[i][k].EventName != tempArra[i+1][L].EventName){
NewArray.push(tempArra[i+1][L]);
}else{
if(tempArra[i][k] != undefined){
CommonArray.push(tempArra[i][k]) ;
}
if(tempArra[i+1][L] != undefined){
CommonArray.push(tempArra[i+1][L]);
}
}
}
}
}