Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering an object array with duplicate titles and unique description?

I need to filter an object array that contains duplicate titles but the description is unique. For e.g

[
    {
        "Title": "New York",
        "Description": "A healthy and modernized transit system"
    },
    {
        "Title": "New York",
        "Description": "changed transit system"
    },
    {
        "Title": "New York",
        "Description": "xyz"
    },
    {
        "Title": "New York",
        "Description": "abc"
    },
    {
        "Title": "chicago",
        "Description": "jdfjjfj"
    },
    {
        "Title": "chicago",
        "Description": "abcdfdjf"
    }
]

As you can see, the titles are duplicate whereas its description is unique.So can anybody please tell me how to filter this object array which filters out unique title and description being unique.

Basically the filtering should be as such that title comes first with its following unique descriptions.

like image 528
Sumodh Nair Avatar asked Nov 02 '22 17:11

Sumodh Nair


1 Answers

var rs = {};
$.each(objs, function(i, obj) {
  if (rs[obj.Title] === undefined) rs[obj.Title] = [];
  rs[obj.Title].push(obj.Description);
});

Check it on jsFiddle: http://jsfiddle.net/U6qu4/

like image 87
jgroenen Avatar answered Nov 09 '22 09:11

jgroenen