使用AngularJS对重复的JSON数据进行分组

时间:2016-02-12 04:42:08

标签: angularjs json

请帮助我,我有一个重复的JSON对象:

[{
    "nip": "6080022A",
    "nama": "ASLAN PURNAMA",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": "Potensial",
    "semester1_2015": null
}, {
    "nip": "6080022A",
    "nama": "ASLAN PURNAMA",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": null,
    "semester1_2015": "Potensial"
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": "Potensial",
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": null,
    "semester1_2015": null
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": "Potensial",
    "semester1_2014": null,
    "semester2_2014": null,
    "semester1_2015": null
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": "Potensial",
    "semester2_2014": null,
    "semester1_2015": null
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": "Potensial",
    "semester1_2015": null
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": null,
    "semester1_2015": "Potensial"
}]

我想使用AngularJS显示值,我期待的最终结果将是这样的:

nip : 6080022A
nama : ASLAN PURNAMA
semester1_2012 : null
semester2_2012 : null
semester1_2013 : null
semester2_2013 : null
semester1_2014 : null
semester2_2014 : Potensial
semester1_2015 : Potensial

nip : 6080022A
nama : NURHAYATI
semester1_2012 : null
semester2_2012 : null
semester1_2013 : Potensial
semester2_2013 : Potensial
semester1_2014 : Potensial
semester2_2014 : Potensial
semester1_2015 : Potensial

我尝试使用AngularJS Unique Filter格式化结果,但结果只过滤了" nip"和"名称"

1 个答案:

答案 0 :(得分:0)

请检查此example(查看控制台)。我使用了非常酷的库linq.js。只需以这种方式处理初始数组,并将结果数组与ng-repeat一起使用,例如。

<强>解决方案:

$(function(){
   var array = [{
    "nip": "6080022A",
    "nama": "ASLAN PURNAMA",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": "Potensial",
    "semester1_2015": null
}, {
    "nip": "6080022A",
    "nama": "ASLAN PURNAMA",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": null,
    "semester1_2015": "Potensial"
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": "Potensial",
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": null,
    "semester1_2015": null
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": "Potensial",
    "semester1_2014": null,
    "semester2_2014": null,
    "semester1_2015": null
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": "Potensial",
    "semester2_2014": null,
    "semester1_2015": null
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": "Potensial",
    "semester1_2015": null
}, {
    "nip": "6080048A",
    "nama": "NURHAYATI",
    "semester1_2012": null,
    "semester2_2012": null,
    "semester1_2013": null,
    "semester2_2013": null,
    "semester1_2014": null,
    "semester2_2014": null,
    "semester1_2015": "Potensial"
}];            
   var res = Enumerable.From(array).GroupBy(function (x) { return x.nip + '&' + x.nama;} ).Select(function (x) { return { key: x.Key(), res:x.Aggregate(function(a,b){
     var res={};
     for(p in a)
        if(p!= 'nip' && p != 'nama')
            res[p]=a[p];
      for(p in b)
        if(p!= 'nip' && p != 'nama' && a[p]==null && b[p]!=null)
            res[p]=b[p];            

       return res;
   }) } }).ToArray().map(function(x){
     x.res.nip=x.key.split('&')[0];
     x.res.nama=x.key.split('&')[1];
     return x.res;
   });
   console.log(res);  
})