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

使用优秀的underscore.js库(它的微型库,只有 4 kB,并为 javascript 提供了许多强制性的有用补充)。

其次,您说您需要以下输出:

{ "city":"Paris",
    {{"name":"John Smith","color":"orange"},
     {"name":"Tommy Jones","color":"blue"}}
}

这在语法上是错误的。人是一个对象数组,它应该表示为[{ "name" : ... }]而不是 {{ "name" : }}。之后"city" : "Paris",你必须有一把钥匙给人民,它只是不能{{在城市之后开始。所以你的输出应该是这样的:

[
  { "city" : "Paris", "people" : [{"name" : "John Smith" }, ... ] },
  { "city" : ... },
  { "city" : ... }
]

除此之外,您的问题可以使用 underscorejs 解决:

// Assume the JSON array of people is in a variable called data
// Group data by city
data_by_city = _.groupBy(data, function(person) {
  return person["favorites"]["city"]; // Data will be grouped by this key
});

// Data is now present as { "Paris" : [ people ], ... }
// Let's convert it to [{ "city" : "Paris", "people" : [ people ] ...
output = _.map(data_by_city, function(people, city) {
  return { "city" : city, "people" : people };
});
于 2013-05-24T23:26:05.003 回答
0

派对有点晚了,但你走吧!(虽然不如罗斯彭曼的好!) 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 回答