Ich habe drei API-Endpunkte, wobei api2 und api3 beide einen Fremdschlüssel haben, der api1 id entspricht.Angular, verschmelzen drei Arrays zu einem, wo id übereinstimmen
Für jetzt dies ist mein Code, zu tun, was ich will (Verbinden Sie die Objekte mit der ID in ein neues Array entspricht), aber ich würde etwas Hilfe, um es sauberer zu bekommen:
$scope.arr = function() {
$scope.cars = [];
$scope.information = [];
$scope.booking = [];
$scope.newArr = [];
$http({method: 'GET', url: '*api1*'}).success(function(data) {
angular.forEach(data, function(value, key) {
$scope.cars.push(value);
})
$http({method: 'GET', url: '*api2*'}).success(function(data){
angular.forEach(data, function(value, key) {
$scope.information.push(value);
})
for(var i = 0, car; car = $scope.cars[i]; i++) {
for(var j = 0, information; information = $scope.information[j]; j++) {
if(car.id === information.carId){
$scope.newArr.push({
"name" : car.name,
"city": information.city,
"mile": information.mile,
})
}
}
}
$http({method: 'GET', url: '*api3*'}).success(function(data){
angular.forEach(data, function(value, key) {
$scope.booking.push(value);
})
for(var i = 0, car; car = $scope.cars[i]; i++) {
for(var j = 0, booking; booking = $scope.booking[j]; j++) {
if(car.id === booking.carId){
$scope.newArr.push({
"book" : booking.isBookable,
})
}
}
}
})
})
})
}
$scope.arr();
Es fühlt sich an wie ein Durcheinander, aber der einzige Weg, mit dem ich es geschafft habe. Gibt es einen anderen Ansatz, der das Ganze viel einfacher macht?
Blick in Drittanbieter-Bibliotheken, verwende ich [Strich] (http://underscorejs.org/#union) – George
Unterstrichen oder lodash sind beide gut (_.union(), _.intersection() und so weiter) – rrd
Ich werde sie mir ansehen. Vielen Dank! – Kazordomo