1

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

私は、Country の個別の値とその国の人数を使用して、これから 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

これを行う方法を知っている人はいますか?

4

1 に答える 1

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
    }
}
于 2013-02-20T09:28:46.510 に答える