[英]How to combine result of two each iterators into one JSON array of objects
[英]How to combine two or multiple JSON objects into one in AngularJS?
是否可以在AngularJS的API JSON響應中添加新數據? 我正在使用一個REST API,該API返回國家/地區詳細信息,但是我想為其添加更多詳細信息。
空字段很少(紅色箭頭)。 我需要在每個國家/地區使用另一個API填充它,並在可能的情況下添加新的鍵,例如“ id”等。
有可能實現這一目標嗎? 如何?
這是代碼:
$http.get("https://api.../getfile/rohit/fw18countries").success(function(data) {
teams = data.teams;
//console.log(data);
/* shuffle(teams); */
});
return {
GetTeams: function(){
return teams;
},
get: function(teamId) {
for (var i = 0; i < teams.length; i++) {
if (teams[i].id === parseInt(teamId)) {
return teams[i];
}
}
return null;
}
}
/* all teams data */
$scope.Allteams = wcAPI.GetTeams();
/* REST API call */
$http.get("http://api.../v1/competitions/467/fixtures")
.then(function(data) {
$scope.country = data;
console.log($scope.country);
});
因此,現在,我需要使用$scope.Allteams
(我的靜態數據)和上述各個字段來更新$scope.country
(REST API數據)。 需要分別合並它們。
您可以使用以下代碼嘗試此操作。 遍歷數據數組並找到正確的數組。 然后將所有值添加到對象。
for(var i = 0; i< $scope.country.length; i++) {
//Find the right index from $scope.country
for(var j = 0; j< $scope.Allteams.length; j++) {
// TODO here you have to campare by right value
if($scope.country[i].id == $scope.Allteams[j].teamId) {
for(var key in $scope.AllTeams[j]) {
if(!$scope.Allteams[j].hasOwnProperty(key)) continue;
//Adds the value to the object
$scope.country[i][key] = $scope.Allteams[j][key];
}
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.