Skip to content

Instantly share code, notes, and snippets.

@steinbring
Created August 10, 2014 16:27
Show Gist options
  • Save steinbring/8ff2396e6d913a23b12b to your computer and use it in GitHub Desktop.
Save steinbring/8ff2396e6d913a23b12b to your computer and use it in GitHub Desktop.
Take an array of stations (with marketCity as an attribute) and create an array of marketCities (with stations as an attribute)
// Define the unique market cities
$scope.marketCities = [];
for (var i = 0; i < data.query.results.station.length; i++){
// if it isn't already there, add it
if(i !== 0 && data.query.results.station[i].marketCity !== data.query.results.station[i-1].marketCity){
$scope.marketCities.push({ label: data.query.results.station[i].marketCity, stations: [data.query.results.station[i]]});
}else if(i == 0){
$scope.marketCities.push({ label: data.query.results.station[i].marketCity, stations: [data.query.results.station[i]]});
}else if(data.query.results.station[i].id !== data.query.results.station[i-1].id){
// is the market already there? well, just add the station then.
$scope.marketCities[$scope.marketCities.length-1].stations.push(data.query.results.station[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment