I have a JavaScript object occurrences;
var occurrences = { "Karri" : 1, "Ismo" : 1, "Harri": 4, ........} //it has 129 elements
I want to have a JavaScript object which looks like this:
var json = [{"Researcher":"Karri","Total":1},
{"Researcher":"Ismo","Total":1},
{"Researcher":"Harri","Total":4},......]
Any ideas about how to do it?
I have this method where I count the total numbers and I try to create JavaScript object.
function countPublicationsPerResearcher(fullnames){
var occurrences = { };
var json =[];
for (var i = 0; i < fullnames.length; i++) {
if (typeof occurrences[fullnames[i]] == "undefined") {
occurrences[fullnames[i]] = 1;
json.push({ "Researcher": fullnames[i],
"Total": occurences[fullnames[i]]
});
} else {
occurrences[fullnames[i]]++;
json.push({ "Researcher": fullnames[i],
"Total": occurrences[fullnames[i]]
});
}
}
//console.log(JSON.stringify(occurrences));//prints the occurences Json
console.log(JSON.stringify(json)); //prints every iteration of the for loop, not overall result
}