2012-11-27 8 views
6

Tastypie zwraca tablicę, zagnieżdżonych zasobów w sposób następujący:underscore.js - _.groupBy zagnieżdżone atrybutów

data = [ 

{"adult_price": "123", "child_price": "123", "currency": [{"abbrev": "USD", "id": "1", "name": "US Dollars", "resource_uri": "/api/v1/currency/1/", "symbol": "$"}], "day": [{"day_of_week": "TUE", "id": "2", "resource_uri": "/api/v1/days/2/"}], "description": "Please enter the tour description here", "id": "1", "important": "ex. Please contact us to negotiate a price if you want to book the Fiat for 1 person only.", "location": [{"id": "1", "name": "Dublin", "resource_uri": "/api/v1/location/1/"}], "name": "test dublin", "resource_uri": "/api/v1/tours/1/", "start_time": "23:03:51", "subtitle": "ex. These prices include... but not...", "teenager_student_price": "123", "under_6_price": "123"}, 

{"adult_price": "22", "child_price": "22", "currency": [{"abbrev": "USD", "id": "1", "name": "US Dollars", "resource_uri": "/api/v1/currency/1/", "symbol": "$"}], "day": [{"day_of_week": "WED", "id": "3", "resource_uri": "/api/v1/days/3/"}], "description": "Please enter the tour description here", "id": "2", "important": "ex. Please contact us to negotiate a price if you want to book the Fiat for 1 person only.", "location": [{"id": "2", "name": "Venice", "resource_uri": "/api/v1/location/2/"}], "name": "test Venice", "resource_uri": "/api/v1/tours/2/", "start_time": "23:09:01", "subtitle": "ex. These prices include... but not...", "teenager_student_price": "22", "under_6_price": "22"}, 

{"adult_price": "22", "child_price": "222", "currency": [{"abbrev": "USD", "id": "1", "name": "US Dollars", "resource_uri": "/api/v1/currency/1/", "symbol": "$"}], "day": [{"day_of_week": "MON", "id": "1", "resource_uri": "/api/v1/days/1/"}, {"day_of_week": "TUE", "id": "2", "resource_uri": "/api/v1/days/2/"}, {"day_of_week": "WED", "id": "3", "resource_uri": "/api/v1/days/3/"}], "description": "Please enter the tour description here", "id": "3", "important": "ex. Please contact us to negotiate a price if you want to book the Fiat for 1 person only.", "location": [{"id": "3", "name": "Rome", "resource_uri": "/api/v1/location/3/"}], "name": "test Rome", "resource_uri": "/api/v1/tours/3/", "start_time": "23:15:09", "subtitle": "ex. These prices include... but not...", "teenager_student_price": "22", "under_6_price": "222"}, 

{"adult_price": "22", "child_price": "222", "currency": [{"abbrev": "USD", "id": "1", "name": "US Dollars", "resource_uri": "/api/v1/currency/1/", "symbol": "$"}], "day": [{"day_of_week": "MON", "id": "1", "resource_uri": "/api/v1/days/1/"}, {"day_of_week": "WED", "id": "3", "resource_uri": "/api/v1/days/3/"}], "description": "Please enter the tour description here", "id": "4", "important": "ex. Please contact us to negotiate a price if you want to book the Fiat for 1 person only.", "location": [{"id": "3", "name": "Rome", "resource_uri": "/api/v1/location/3/"}], "name": "test Rome 2", "resource_uri": "/api/v1/tours/4/", "start_time": "01:01:11", "subtitle": "ex. These prices include... but not...", "teenager_student_price": "22", "under_6_price": "22"}, {"adult_price": "123", "child_price": "123", "currency": [{"abbrev": "USD", "id": "1", "name": "US Dollars", "resource_uri": "/api/v1/currency/1/", "symbol": "$"}], "day": [{"day_of_week": "TUE", "id": "2", "resource_uri": "/api/v1/days/2/"}, {"day_of_week": "THU", "id": "4", "resource_uri": "/api/v1/days/4/"}], "description": "Please enter the tour description here", "id": "5", "important": "ex. Please contact us to negotiate a price if you want to book the Fiat for 1 person only.", "location": [{"id": "2", "name": "Venice", "resource_uri": "/api/v1/location/2/"}], "name": "test Venice 2", "resource_uri": "/api/v1/tours/5/", "start_time": "01:03:27", "subtitle": "ex. These prices include... but not...", "teenager_student_price": "123", "under_6_price": "123"} 

] 

ja jak wykonać .groupBy że grupy przez położenie [0] atrybutów .names, który powinien zwrócić tablicę 3 tablic. czy to możliwe?

Jak mogę wykonać równowartość:

_.groupBy(data, 'location[0].name'] 

Odpowiedz

16

Drugi argument dla groupBy może być funkcją lub ciąg:

GroupBy_.groupBy(list, iterator)

Dzieli zbiór w zestawy, pogrupowane według wyniku wyświetlania każdej wartości przez iterator. Jeśli iterator jest ciągiem, a nie funkcją, grupy według właściwości o nazwie iterator na każdej z wartości.

Chcesz skorzystać z formularza function ponieważ nie jesteś grupowania na prostym własności najwyższego poziomu:

_(data).groupBy(function(o) { 
    return o.location[0].name; 
}); 

Demo: http://jsfiddle.net/ambiguous/YFKXC/