I have two arrays that i like to compare on the ID, if the ID from one array does not exist in the other i will add it with the Http Post.
This is the build up:
$scope.Games = [{id:1,Name:"BatMan"},
{id:2,Name:"SpiderMan"},
{id:2,Name:"Hulk"}];
$scope.NewGames = [{id:1,Name:"BatMan"},
{id:2,Name:"SpiderMan"},
{id:3,Name:"Hulk"},
{id:4,Name:"DeadPool"},
{id:5,Name:"IronMan"}, ,
{id:6,Name:"DrStrange"}];
so i load all the Games and NewGames with a GET in the two $scopes Now i would like to compare the two on the id_game, so i was thinking of something like this but can't get it to work, the http section works find however without the indexOf, it will add all the games double in DB if they already existed and that is what i want to prevent.
angular.forEach($scope.NewGames, function (value, index) {
if ($scope.Games.indexOf(value.id ) === -1) {
console.log('New game to add' + value.Name)
$http({
method: 'POST',
url: 'http.....\addGame',
data: value
})
}
})