如何计算JSON中的特定元素

时间:2015-04-15 12:16:19

标签: javascript json

我有一个JSON对象,如下所示:

var allDataJson = [{"Researchers":"Laura, Mikael","Year":"2015"}, 
{"Researchers":"Mikko","Year":"2013",}, 
{"Researchers":"Harri","Year":"2015"}, .......] //long list 

我需要创建一个JSON对象,如下所示:

 var myjson = [{"Year": 2015, "Total":60}, 
                  {"Year": 2014, "Total":68},
                  {"Year": 2013, "Total":72}]

例如,2015年有60个allDataJson个项目。

到目前为止,我只能获得年份值。有关如何计算Total的任何想法?

这是我的代码:

function countPublicationsPerYear(allDataJson){
    var lookup = {};
    var items = allDataJson;
    var result = [];

    for (var item, i = 0; item = items[i++];) {
        var year = item.Year;
        count ++;
        if (!(year in lookup)) {
            var count = Object.keys(item).length;
            lookup[year] = 1;
            result.push({ "Year":year
            });
        }
    }
    return result; //returns Year: 2013, 2014, 2015
}

4 个答案:

答案 0 :(得分:1)

所以你试图增加研究人员的总数?

<强>答案

function countPublicationsPerYear(allDataJson){
    var lookup = {};
    var items = allDataJson;
    var result = [];

    for (var item, i = 0; item = items[i++];) {
        var year = item.Year;
        count ++;
        if (!(year in lookup)) {
            lookup[year] = 1;
        } else {
            lookup[year]++;
        }
    }

    for (var property in lookup) {
        if (lookup.hasOwnProperty(property)) {
            result.push({"Year": property, "Total": lookup[property]});
        }
    }

    return result; //returns Year: 2013, 2014, 2015
}

var allDataJson = [{"Researchers":"Laura, Mikael","Year":"2015"}, 
{"Researchers":"Mikko","Year":"2013",}, 
{"Researchers":"Harri","Year":"2015"}];

function countPublicationsPerYear(allDataJson){
        var lookup = {};
        var items = allDataJson;
        var result = [];
    
        for (var item, i = 0; item = items[i++];) {
            var year = item.Year;
            if (!(year in lookup)) {
                lookup[year] = 1;
            } else {
                lookup[year]++;
            }
        }
    
        for (var property in lookup) {
            if (lookup.hasOwnProperty(property)) {
                result.push({"Year": property, "Total": lookup[property]});
            }
        }
    
        alert(JSON.stringify(result));
  
        return result; //returns Year: 2013, 2014, 2015
    }

countPublicationsPerYear(allDataJson);

答案 1 :(得分:0)

我会做这样的事情:

var count = {};
for (var x = 0; x < allDataJson.length; x++) {
    var current = allDataJson[x];
    if (!count[current["Year"]]) count[current["Year"]] = 1;
    else
        count[current["Year"]] = count[current["Year"]] + 1;
}

var result = []
for(key in count){
    result.push({
        "Year" : key,
        "Total" : count[key]
    })
}

console.log(result) //prints [{"Year":"2013","Total":1},{"Year":"2015","Total":2}]

请点击此处:https://jsfiddle.net/

答案 2 :(得分:0)

我最终为你写了这个片段:

var totals = {};
for (var i=0; i<allDataJson.length; i++) {
    var researchYear = allDataJson[i].Year;
  if (researchYear in totals){ 
    totals[researchYear]++;
  } else {
    totals[researchYear] = 1;
  }
 };
console.log(totals); // your years summaries
console.log(JSON.stringify(totals)); //as json

答案 3 :(得分:0)

var allDataJson = [{"Researchers":"Laura, Mikael","Year":"2015"}, 
{"Researchers":"Mikko","Year":"2013"}, 
{"Researchers":"Harri","Year":"2015"},
{"Researchers":"Mikko","Year":"2013"}, 
{"Researchers":"Harri","Year":"2015"},{"Researchers":"Mikko","Year":"2013"}, 
{"Researchers":"Harri","Year":"2015"},{"Researchers":"Mikko","Year":"2013"}, 
{"Researchers":"Harri","Year":"2015"},{"Researchers":"Mikko","Year":"2013"}, 
{"Researchers":"Harri","Year":"2015"}];
var your_array=[];
var counts = {};
$.each(allDataJson, function (key, vale) {
    your_array.push(vale.Year);
});
your_array.forEach(function(x) { 
    counts[x] = (counts[x] || 0)+1;
});
alert(JSON.stringify(counts));  

demo