Skip to content Skip to sidebar Skip to footer

Counting Certain Json Elements

I have a JSON object allDataJson which have around 500 objects and looks like this: [{'Researchers':'Karri, Ismo','Year':'2013','Title':'A'}, {'Researchers':'Grigori, Ahmed, Roope

Solution 1:

I have done some coding. Have a look at it and let me know if this is what you wanted :)

var allDataJson = [{"Researchers":"Karri, Ismo","Year":"2013","Title":"A"},
 {"Researchers":"Grigori, Ahmed, Roope, Arto, Daisuke, Arasawa,IkeHama","Year":"2015","Title":"B"},
 {"Researchers":"Grigori, Ahmed, Roope, Arto, Daisuke","Year":"2015","Title":"C"},
{"Researchers":"Grigori, Ahmed, Roope, Arto, Daisuke","Year":"2014","Title":"D"}];


var formatted = allDataJson.reduce(function (prev, value) {

    var researcher,
        researcherObj,
        researchers = value.Researchers.split(',');
    for (var i = 0; i < researchers.length; i++) {

        researcher = researchers[i].trim();
        researcherObj = prev[researcher];
        if (researcherObj) {
            if (researcherObj[value.Year]) {
                researcherObj[value.Year]++;
            } else {
                researcherObj[value.Year] = 1;
            }
        } else {
            researcherObj = {};
            researcherObj[value.Year] = 1;
            prev[researcher] = researcherObj;
        }

    }

    return prev;

}, {});

var count,
    detail,
    output = [],
    published;
for (var researcher in formatted) {

    count = 0;
    published = [];
    if (formatted.hasOwnProperty(researcher)) {
        detail = {
            Researcher: researcher
        };
        researchDetails = formatted[researcher];
            
        for (var researchDetail in researchDetails) {
    
            if (researchDetails.hasOwnProperty(researchDetail)) {
                count += researchDetails[researchDetail];
                published.push([parseInt(researchDetail, 10), researchDetails[researchDetail]]);
            }
        
        }
        
        detail.Published = published;
        detail.Total = count;
        output.push(detail);
    
    }
    
}

console.log(JSON.stringify(output));
alert(JSON.stringify(output));

Modify this logic as per your need :)

Solution 2:

Hi may be this will be a start,

functionFlowChartJson(fullnames, allDataJson){

    var occurences = { };
    var json =[];

    for (var i = 0; i < fullnames.length; i++) {
        if (typeof occurences[fullnames[i]] == "undefined") {
            occurences[fullnames[i]] = 1;
        } else {
            occurences[fullnames[i]]++;
        }
    }

    for(var name in occurences){
    	var published = [];
    	for(var j =0;j< allDataJson.length;j++) {
    		if (allDataJson[j].Researcher == name ) {
    			published.push([allDataJson[j].Year, 1]); //I'm assuming 1 is a constant here. cos I dont understand
    		}
    	}
        json.push({
        	"Published":published,
			"Researcher":name,
            "Total":occurences[name]
        });

    }
    return json;
} 

Post a Comment for "Counting Certain Json Elements"