JavaScript - 按类似属性查找对象并将其推送到新数组中

Ham*_* L. 2 javascript arrays json

我有以下JSON树:

[
  {
    "category":"PASTAS",
    "createdAt":"2016-01-01T19:47:57.813Z",
    "currency":"$",
    "dishName":"Spaghetti",
    "estTime":"10-20 min",
    "price":10,
    "subName":"Pasta",
    "updatedAt":"2016-04-28T20:48:06.800Z"
  },
  {
    "category":"PIZZAS",
    "createdAt":"2016-04-19T21:44:56.285Z",
    "currency":"$",
    "dishName":"Ai Funghi Pizza ",
    "estTime":"20-30 min",
    "price":20,
    "subName":"Pizza",
    "updatedAt":"2016-04-28T20:58:39.499Z"
  },
  {
    "category":"PIZZAS",
    "createdAt":"2016-04-19T21:44:56.285Z",
    "currency":"$",
    "dishName":"Seafood Pizza",
    "estTime":"20-30 min",
    "price":10,
    "subName":"Pizza",
    "updatedAt":"2016-04-28T20:58:39.499Z"
  }
]
Run Code Online (Sandbox Code Playgroud)

正如您在JSON树中看到的那样,元素category:"PIZZAS"重复两次,我想要做的是创建一个新数组或组织这些结果以避免在所有其他重复项中重复,即在上面的示例中,会得到这样的最终结果:

 Pastas:
 Spaghetti

 Pizza:
 Ai Fungi Pizza,
 Seafood Pizza
Run Code Online (Sandbox Code Playgroud)

关于如何实现想要结果的任何想法?

Cer*_*rus 7

假设数组已命名data,这应该可以解决问题:

var result = {};                                       // Create an output object.
for(var i = 0; i < data.length; i++){                  // Loop over the input array.
    var row = data[i];                                 // Store the current row for convenience.
    result[row.category] = result[row.category] || []; // Make sure the current category exists on the output.
    result[row.category].push(row.dishName);           // Add the current dish to the output.
}
Run Code Online (Sandbox Code Playgroud)