The idea is to take all the "Locations" and merge them together to give out a number for my chart to read. So lets say you have 5 "Locations" that are New York with varying "Dates". I want to merge all 5 together and output a number of 5, as well as the merged "Punches" from the .json. Right now I have it grab Locations, and Merge similar Punches. But I want to get total number of instances of New York for example and output that number as well.
我们的想法是采取所有“地点”并将它们合并在一起,以便为我的图表提供一个数字。因此,假设您有5个“位置”,纽约有不同的“日期”。我想将所有5个合并在一起并输出5个数字,以及.json中合并的“Punches”。现在我有它抓住Locations,并合并类似的拳击。但我想以纽约的实例总数为例,并输出该数字。
chart.json
[
{
"Date":"2003",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2003",
"Punches":"1",
"Locations":"New York"
},
{
"Date":"2004",
"Punches":"0",
"Locations":"Chicago"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Chicago"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Ohio"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Ohio"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"Ohio"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"Florida"
},
{
"Date":"2009",
"Punches":"1",
"Locations":"Florida"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2009",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2009",
"Punches":"0",
"Locations":"Chicago"
},
{
"Date":"2010",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2010",
"Punches":"0",
"Locations":"Florida"
}
]
JS
function LocationMerge()
{
$.ajax(
{
url: 'data.json',
data:{},
dataType: 'json',
success: function(data)
{
var string = JSON.stringify(data);
var objects = $.parseJSON(string);
var categories = new Array();
var mergedPieces = new Array();
var i = 0;
_.each(objects, function(obj)
{
var existingObj;
if ($.inArray(obj.Locations, categories) >= 0)
{
existingObj = _.find(objects, function(o)
{
return o.Locations=== obj.Locations;
});
existingObj["Punches"] += obj["Punches"];
}
else
{
mergedPieces[i] = obj;
categories[i] = obj.Locations;
i++;
}
});
mergedPieces = _.sortBy(mergedPieces, function(obj)
{
return obj["Punches"];
}).reverse();
_.each(mergedPieces, function(obj)
{
var output = '';
_.each(obj, function(val, key)
{
output += key + ': ' + val + '<br>';
});
output += '<br>';
console.log(output);
});
}
});
}
2 个解决方案
#1
0
try this in your success function. check out the demo (console output) for the result
在你的成功功能中试试这个。查看结果的演示(控制台输出)
var data = [{"Date": "2003", "Punches": "0", "Locations": "New York"}, {"Date": "2003", "Punches": "1", "Locations": "New York"}, {"Date": "2004", "Punches": "0", "Locations": "Chicago"}, {"Date": "2004", "Punches": "1", "Locations": "Chicago"}, {"Date": "2004", "Punches": "1", "Locations": "Ohio"}, {"Date": "2004", "Punches": "1", "Locations": "Ohio"}, {"Date": "2007", "Punches": "0", "Locations": "Ohio"}, {"Date": "2007", "Punches": "0", "Locations": "Florida"}, {"Date": "2009", "Punches": "1", "Locations": "Florida"}, {"Date": "2007", "Punches": "0", "Locations": "New York"}, {"Date": "2009", "Punches": "0", "Locations": "New York"}, {"Date": "2009", "Punches": "0", "Locations": "Chicago"}, {"Date": "2010", "Punches": "0", "Locations": "New York"}, {"Date": "2010", "Punches": "0", "Locations": "Florida"}];
function LocationMerge()
{
var newObj = new Object();
_.each(data, function(obj){
if(newObj[obj.Locations] === undefined)
newObj[obj.Locations] = {"Location":obj.Locations,"Punches":parseInt(obj.Punches),"items":1};
else{
newObj[obj.Locations]["Punches"]+=parseInt(obj.Punches);
newObj[obj.Locations]["items"]++;
}
});
console.log(newObj);
}
LocationMerge();
#2
1
Code below
var dataset = [
{
"Date":"2003",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2003",
"Punches":"1",
"Locations":"New York"
},
{
"Date":"2004",
"Punches":"0",
"Locations":"Chicago"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Chicago"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Ohio"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Ohio"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"Ohio"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"Florida"
},
{
"Date":"2009",
"Punches":"1",
"Locations":"Florida"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2009",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2009",
"Punches":"0",
"Locations":"Chicago"
},
{
"Date":"2010",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2010",
"Punches":"0",
"Locations":"Florida"
}
];
map = {};
dataset.forEach(function(data){ if(map[data.Locations]){map[data.Locations]=map[data.Locations]+1}else{map[data.Locations]=1}});
snippet.log(map)
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
#1
0
try this in your success function. check out the demo (console output) for the result
在你的成功功能中试试这个。查看结果的演示(控制台输出)
var data = [{"Date": "2003", "Punches": "0", "Locations": "New York"}, {"Date": "2003", "Punches": "1", "Locations": "New York"}, {"Date": "2004", "Punches": "0", "Locations": "Chicago"}, {"Date": "2004", "Punches": "1", "Locations": "Chicago"}, {"Date": "2004", "Punches": "1", "Locations": "Ohio"}, {"Date": "2004", "Punches": "1", "Locations": "Ohio"}, {"Date": "2007", "Punches": "0", "Locations": "Ohio"}, {"Date": "2007", "Punches": "0", "Locations": "Florida"}, {"Date": "2009", "Punches": "1", "Locations": "Florida"}, {"Date": "2007", "Punches": "0", "Locations": "New York"}, {"Date": "2009", "Punches": "0", "Locations": "New York"}, {"Date": "2009", "Punches": "0", "Locations": "Chicago"}, {"Date": "2010", "Punches": "0", "Locations": "New York"}, {"Date": "2010", "Punches": "0", "Locations": "Florida"}];
function LocationMerge()
{
var newObj = new Object();
_.each(data, function(obj){
if(newObj[obj.Locations] === undefined)
newObj[obj.Locations] = {"Location":obj.Locations,"Punches":parseInt(obj.Punches),"items":1};
else{
newObj[obj.Locations]["Punches"]+=parseInt(obj.Punches);
newObj[obj.Locations]["items"]++;
}
});
console.log(newObj);
}
LocationMerge();
#2
1
Code below
var dataset = [
{
"Date":"2003",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2003",
"Punches":"1",
"Locations":"New York"
},
{
"Date":"2004",
"Punches":"0",
"Locations":"Chicago"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Chicago"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Ohio"
},
{
"Date":"2004",
"Punches":"1",
"Locations":"Ohio"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"Ohio"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"Florida"
},
{
"Date":"2009",
"Punches":"1",
"Locations":"Florida"
},
{
"Date":"2007",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2009",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2009",
"Punches":"0",
"Locations":"Chicago"
},
{
"Date":"2010",
"Punches":"0",
"Locations":"New York"
},
{
"Date":"2010",
"Punches":"0",
"Locations":"Florida"
}
];
map = {};
dataset.forEach(function(data){ if(map[data.Locations]){map[data.Locations]=map[data.Locations]+1}else{map[data.Locations]=1}});
snippet.log(map)
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>