2012-11-27 15 views
6

Tastypie restituisce un array, comprese le risorse annidate come segue:underscore.js - _.groupBy attributo nidificato

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"} 

] 

Vorrei eseguire un .groupBy che gruppi per la posizione [0] attribuiscono .names, che dovrebbe restituire una matrice di 3 array. È possibile?

Come posso eseguire l'equivalente di:

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

risposta

16

Il secondo argomento per groupBy può essere una funzione o di una stringa:

groupBy_.groupBy(list, iterator)

divide una collezione in set, raggruppati in base al risultato dell'esecuzione di ciascun valore tramite iteratore. Se iteratore è una stringa anziché una funzione, raggruppa in base alla proprietà denominata da iteratore su ciascuno dei valori.

si desidera utilizzare il modulo di funzione dal momento che non stai raggruppamento su una semplice proprietà di livello superiore:

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

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