On Wed, Dec 17, 2008 at 9:49 AM, alpha tester <david.oli...@rbs.co.uk> wrote:
>
>
> Hi I'm just learning JQuery and while I've got my head around the general
> concepts, the real power of the logic it provides is still escaping me.
>
> I've got a page with a JSON dataset like this:
>
> var myData =
> { records : [
> { CATEGORY : "Sport", TITLE : "The world of sport", LINK: "http://test.com";
> },
> { CATEGORY : "Sport", TITLE : "More sport", LINK: "http://test.com"; },
> { CATEGORY : "News", TITLE : "News views", LINK: "http://test.com"; },
> { CATEGORY : "News", TITLE : "Some more news", LINK: "http://test.com"; },
> { CATEGORY : "Events", TITLE : "Big Events", LINK: "http://test.com"; },
> { CATEGORY : "Events", TITLE : "Small Events", LINK: "http://test.com"; },
> ]}
>
> Now this is being built into a nested list using Jquery, however at the
> moment I'm hardcoding the category names in my script and looping through
> each title.
>
> I'm now hitting increasing issues as if categories are added/removed it
> become a chore to update.
>
> Can anyone tell me if it's possible to automatically generate a list of
> unique categories?
>
> ie so it'd produce this:
>
> <ul>
> <li>Sport</li>
> <li>News</li>
> <li>Events</li>
> </ul>
>
> Not:
> <ul>
> <li>Sport</li>
> <li>Sport</li>
> <li>News</li>
> <li>News</li>
> <li>Events</li>
> <li>Events</li>
> </ul>
>
> --
> View this message in context: 
> http://www.nabble.com/Make-%3Cli%3E-list-of-unique-json-data--tp21054524s27240p21054524.html
> Sent from the jQuery General Discussion mailing list archive at Nabble.com.
>
>

var categories = {};

$.each(myData.records, function(i, record)
{
        if (!categories[record.CATEGORY]) categories[record.CATEGORY] = [];
                
        categories[record.CATEGORY].push({ title: record.TITLE, link: 
record.LINK });
});

Sport:
        0:
                title: The world of sport
                link: http://test.com

        1:
                title: More sport
                link: http://test.com


News:
        0:
                title: News views
                link: http://test.com

        1:
                title: Some more news
                link: http://test.com


Events:
        0:
                title: Big Events
                link: http://test.com

        1:
                title: Small Events
                link: http://test.com

Reply via email to