1

次のような JSON オブジェクトがあります。

data=[{"name":"John Smith",
       "favorites":{"color":"orange",
                    "city":"Paris"}},
      {"name":"Jane Baker",
       "favorites":{"color":"red",
                    "city":"San Francisco"}},
      {"name":"Tommy Jones",
       "favorites":{"color":"blue",
                    "city":"Paris"}}]

jQuery を使用してこれらの結果を調べ、各人がお気に入りの都市オブジェクトに入るデータを再構築したいと思います (固有の場合は新しいオブジェクトを作成し、そうでない場合は既存のオブジェクトに入れます)。このような...

data=[
    {"city":"Paris",
        {{"name":"John Smith",
          "color":"orange"},
         {"name":"Tommy Jones",
          "color":"blue"}}
    },
    {"city":"San Francisco",
        {{"name":"Jane Baker",
          "color":"red"}}
    }]

誰かがこれについてどうやって私を助けてくれますか?

4

4 に答える 4

4

私はそれを次のようにします:

var newData = [], tempData = {};

for (var i = 0; i < data.length; i++) {

  // Extract all the information we need.
  var person = data[i],
      cityName = person.favorites.city,
      personObj = {name: person.name, color: person.favorites.color };

  if (tempData[cityName]) {
    tempData[cityName].people.push(personObj); 
  } else {
    tempData[cityName] = { people: [personObj] };
  }
}

// Sort the data into the required format
for (var city in tempData) {
  var people = tempData[city].people;
  newData.push({ city: city, people: people });
}
于 2013-05-24T23:00:48.323 に答える
1

このメソッドは、次のオブジェクトを生成します

{"Paris":[
    {"name":"John Smith","color":"orange"},
    {"name":"Tommy Jones","color":"blue"}],
"San Francisco":[
    {"name":"Jane Baker","color":"red"}]
}

ここにコードがあります

var data=[{"name":"John Smith","favorites":{"color":"orange","city":"Paris"}},
{"name":"Jane Baker","favorites":{"color":"red","city":"San Francisco"}},
{"name":"Tommy Jones","favorites":{"color":"blue","city":"Paris"}}];

var cityData = {};

data.map(function(person){
    if(person.favorites.city in cityData) {
        cityData[person.favorites.city].push(
            {
                "name" : person.name,
                "color" : person.favorites.color
            }
        );
    } else {
        cityData[person.favorites.city] = 
            [{
                "name" : person.name,
                "color" : person.favorites.color
            }];
    }
}, cityData);

console.log(cityData);
console.log(JSON.stringify(cityData));
于 2013-05-24T23:32:52.143 に答える
0

パーティーに少し遅れましたが、どうぞ!(ただし、Ross Penman のものほど良くはありません!) http://jsfiddle.net/Robodude/UzpfQ/

var people =[
    {"name":"John Smith","favorites":{"color":"orange","city":"Paris"}},
    {"name":"Jane Baker","favorites":{"color":"red","city":"San Francisco"}},
    {"name":"Tommy Jones","favorites":{"color":"blue","city":"Paris"}}
];

var cities = [];

for (var i = 0; i < people.length; i++)
{
    var person = people[i];

    var city = person.favorites.city;

    var index = null;
    for (var n = 0; n < cities.length; n++)
    {
          var c = cities[n];
        if (c.city == city)
        {
            index = n;
            break;
        }
    }

    if (index == null)
    {
        var temp = {
            city: city,
            people: [{name: person.name, favorites: [{color: person.favorites.name}]}]
        };

        cities.push(temp);
    }
    else
    {
        cities[index].people.push({name: person.name, favorites: [{color: person.favorites.color}]});
    }

}

console.log(cities);
于 2013-05-24T23:15:35.793 に答える