2

I have the following JSON tree:

[
  {
    "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"
  }
]

As you can see in the JSON tree the element category:"PIZZAS" repeats two times, what I would like to do is to either create a new array or organize these results in a way to avoid repetition in all the other duplicates, i.e. in the example above, I would have a final results like this:

 Pastas:
 Spaghetti

 Pizza:
 Ai Fungi Pizza,
 Seafood Pizza

Any ideas on how to achieve the wanted result?

Seth
  • 10,198
  • 10
  • 45
  • 68
Hamza L.
  • 1,783
  • 4
  • 25
  • 47
  • So, you want to group the elements by `category`? – Cerbrus May 03 '16 at 13:51
  • Yep, you can say so! – Hamza L. May 03 '16 at 13:52
  • Please add real JSON to make others lives easier. – Uzbekjon May 03 '16 at 13:52
  • You can sort your array of objects like this: http://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript After this you neet to just iterate on the array, and if the category is not the previous category.... – vaso123 May 03 '16 at 13:53
  • @Uzbekjon it's the real one! I only shared the first 3 objects – Hamza L. May 03 '16 at 13:54
  • Did you see ][Elminating duplicates in a JSON object](http://stackoverflow.com/questions/6518651/elminating-duplicates-in-a-json-object) ? – hmd May 03 '16 at 13:55
  • I would always recommend trying to solve the problem your self first :) And then ask questions if you failed. Now you haven't really had the learning experience. – Kiksen May 03 '16 at 13:55
  • @Kiksen who told you that, I've already spent several hours on that, with no successful results – Hamza L. May 03 '16 at 14:38
  • I think @Kiksen gets that impression since you didn't provide any actual code which you used as your attempt to answer your own question. Adding the code you attempted is a standard practice (and unspoken requisite) for quality questions. – Seth May 03 '16 at 14:56
  • But they are not duplicate – Redu May 03 '16 at 14:57

2 Answers2

7

Assuming the array is named data, this should do the trick:

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.
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

lodash#groupBy is exactly what you want.

lodash.groupBy(foods, function(food) { return food.category; });

Here's an example with your JSON:

http://codepen.io/damongant/pen/xVQBXG

Aurelia
  • 1,052
  • 6
  • 28
  • since es2015 isn't 100% adopted yet, it never hurts to provide more elaborate answers with legacy (still very popular) JavaScript. – Seth May 03 '16 at 14:12
  • Looking at the OP's output code, it looks like he wants: `{ "PASTAS": ["Spaghetti"], "PIZZAS": ["Ai Funghi Pizza ", "Seafood Pizza"] }` – Cerbrus May 03 '16 at 14:15
  • @Seth lodash is available for ES2015, the syntax isn't, I'll edit that in a second. – Aurelia May 03 '16 at 14:26
  • @Gant the syntax is precisely what I'm referring to. The fat arrow function with implicit return, in particular. – Seth May 03 '16 at 14:33