将 json 数组的一部分分别排序到同一数组的其他部分

Sort one Part of json array respectively to other part of same array

本文关键字:数组 其他部 排序 一部分 json      更新时间:2023-09-26

我有一个 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 中将其排序到下面的数组中。"事件名称"字段应分别用于"01-10 天"和"10-20 天"等"年龄"字段。我制作并尝试了这个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: []}
]

提前致谢

最后一个 for 循环中,您的代码中有一些错误(变量计数未正确使用)在这里查看演示 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]);
                }
            }
        }
    }
}