输出使用Angular.JS和ng-table汇总值

时间:2014-09-30 05:50:00

标签: javascript json angularjs ngtable

我得到了JSON:

 var   scholars = [{"FirstName":"John","LastName":"Doe","Unit":"PA","Institution":"University of Haifa","teken":1,"FirstYearActive":"2007","hIndex":"3","j2014":0,"j2013":4,"j2012":3,"j2011":0,"j2010":0,"j20052009":2,"j20002004":0,"j19901999":0,"jPriorto1990":0,"ab2014":0,"ab2013":0,"ab2012":0,"ab2011":0,"ab2010":0,"ab20052009":0,"ab20002004":0,"ab19902000":0,"abPriorto1990":0,"c2014":5,"c2013":10,"c2012":7,"c2011":0,"c2010":3,"c20052009":2,"c20002004":0,"c19901999":0,"cPriorto1990":0,"b2014":0,"b2013":0,"b2012":0,"b2011":0,"b2010":0,"b20052009":0,"b20002004":0,"b19902000":0,"bPriorto1990":0,"YearsActive":"8","HIE":"72.9"},
{"FirstName":"John","LastNAme":"Smith","Unit":"G&PT","Institution":"University of Haifa","teken":1,"FirstYearActive":"1996","hIndex":"3","j2014":0,"j2013":0,"j2012":0,"j2011":0,"j2010":0,"j20052009":1,"j20002004":2,"j19901999":2,"jPriorto1990":0,"ab2014":0,"ab2013":0,"ab2012":0,"ab2011":0,"ab2010":0,"ab20052009":0,"ab20002004":0,"ab19902000":0,"abPriorto1990":0,"c2014":3,"c2013":1,"c2012":3,"c2011":5,"c2010":3,"c20052009":2,"c20002004":0,"c19901999":0,"cPriorto1990":0,"b2014":0,"b2013":0,"b2012":0,"b2011":0,"b2010":0,"b20052009":0,"b20002004":0,"b19902000":0,"bPriorto1990":0,"YearsActive":"19","HIE":"25.5"},
{"FirstName":"John","LastName":"Guitar","Unit":"IR","Institution":"University of Haifa","teken":1,"FirstYearActive":"2009","hIndex":"0","j2014":0,"j2013":0,"j2012":0,"j2011":0,"j2010":0,"j20052009":1,"j20002004":0,"j19901999":0,"jPriorto1990":0,"ab2014":0,"ab2013":0,"ab2012":0,"ab2011":0,"ab2010":0,"ab20052009":0,"ab20002004":0,"ab19902000":0,"abPriorto1990":0,"c2014":0,"c2013":0,"c2012":0,"c2011":0,"c2010":0,"c20052009":0,"c20002004":0,"c19901999":0,"cPriorto1990":0,"b2014":0,"b2013":0,"b2012":0,"b2011":0,"b2010":0,"b20052009":0,"b20002004":0,"b19902000":0,"bPriorto1990":0,"YearsActive":"6","HIE":"0"}]

我需要用以下代码总结所有 j2014

var j14sum = 0;
      for (var i in scholars){
        if (scholars[i].Institution == "University of Haifa")
          j14sum += parseInt(scholars[i].j2014);
      };

现在,我如何使用angular.js并使用ng-table输出结果?

非常感谢!

1 个答案:

答案 0 :(得分:0)

好的,我想出了如何解决这个问题。

var reduced = scholars.reduce(function(map,obj){
    if(!(obj.Institution in map)){
        map[obj.Institution] = {
            Institution:obj.Institution,
            j2014: 0
        };
    }
    map[obj.Institution].j2014 += parseInt(obj.j2014);
    return map;
},{});

这里是小提琴:http://jsfiddle.net/axdg1r4t/4/