我有一个JSON对象allDataJson
,它有大约500个对象,如下所示:
[{"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"}]
我需要创建另一个具有以下结构的JSON对象:
var json =[
{"Published": [[2013, 1]],
"Total": 1,
"Researcher": "Karri"},
{"Published": [[2015, 2], [2014, 1]],
"Total": 3,
"Researcher": "Grigori"}
//for all of the researchers]
(例如,Karri在2013年发表了1篇文章,Grigori在2015年发表了2篇文章,在2014年发表了1篇文章等。)
到目前为止,我只能将Total和Researcher名称推送到json对象,如下所示。关于如何将已发布的值推送到Json对象的任何想法?
这是我的代码:
function FlowChartJson(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){
json.push(
{"Researcher":name,
"Total":occurences[name]
});
}
return json;
}
答案 0 :(得分:0)
嗨,这可能是一个开始,
function FlowChartJson(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;
}
&#13;
答案 1 :(得分:0)
我做了一些编码。看看它,让我知道这是否是你想要的:)。
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));
根据您的需要修改此逻辑:)