json文件
{"people":[{"Name":"Ray Parker","Email":"luctus.et.ultrices@natoque.ca","State":"Wyoming","Country":"Tuvalu","Martial Status":"Divorced","Department Name":"Tech Support","Company Name":"Borland","Title":"Dr.","Gender":"Male"},
{"Name":"Davis Hooper","Email":"et.libero.Proin@egetvarius.org","State":"Nunavut","Country":"Bangladesh","Martial Status":"Divorced","Department Name":"Public Relations","Company Name":"Cakewalk","Title":"Mr.","Gender":"Female"}, ...... and so on
我喜欢用这个国家的不同值和该国家的人数制作一个json对象。
我尝试了代码
$(document).ready(function() {
var data = $.ajax({
url: "Data_prop.json",
dataType: "json",
async: false
}).responseText;
var unique = {};
var param = "Country"
$.each(data.people, function() {
if (!unique[this[param]])
unique[this[param]] = [];
unique[this[param]].push(this);
});
for(var d in unique) {
alert(d);
}
});
我在控制台中遇到的错误
TypeError: a is undefined
https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
Line 2
有没有人知道如何做到这一点?
答案 0 :(得分:1)
尝试
function(data)
{
var variables = {};
var param = "Country"
$.each(data.people, function()
{
if (!variables[this[param]])
variables[this[param]] = [];
variables[this[param]].push(this);
});
for(var d in variables) {
// add d to array here
// or do something with d
// variables[d] is the array of people
}
}