使用jQuery组织JSON结果

时间:2013-05-24 22:48:46

标签: jquery json sorting object

我有一个看起来像这样的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 个答案:

答案 0 :(得分: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 });
}

答案 1 :(得分: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));

答案 2 :(得分: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);

答案 3 :(得分:0)

使用优秀的underscore.js库(它的micro,只有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" : ... }
]

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

// 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 };
});