AngularJS compare two (scope) arrays and only POST the 'non-same' result (2018)
AngularJS compare two (scope) arrays and only POST the 'non-same' result (2018)
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
})
}
})
the question is the same, but the approach is different in this case, first of all we are 9(!) years further down the line of programming so there should be more efficient ways. As in this case shown by @amrender singh
– Ewald Bos
6 mins ago
1 Answer
1
Since $scope.Games
is an array of Objects use findIndex()
instead of indexOf()
.
$scope.Games
findIndex()
indexOf()
The findIndex()
method executes the function once for each element present in the array:
findIndex()
If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values). If no such element is found it returns -1. In the example given below:
o
is each object/element of array games
and we check for equality between the id
(o.id) prop for each object of games
array with the findId
. If there is an element in the games array, where the id matches with the findId
, the condition turns true and findIndex
returns the index of that element/object.
o
games
id
games
findId
findId
findIndex
var games = [{id:1,value:"abc"},{id:2,value:"dfg"},{id:2,value:"fdf"}];
var findId = 2;
// when object with id is present in array
if(games.findIndex((o)=>o.id == findId) > -1);
console.log("Found");
findId= 6;
//when object with id is not present in array
if(games.findIndex((o)=>o.id == findId) == -1);
console.log("Not found");
i kinda see where you are going but how would i use this with the if().... ? because there are two array's, the 0.id == 6 in your case also comes from an array, value
– Ewald Bos
22 hours ago
@EwaldBos I have updated my answer please have a look :-)
– amrender singh
22 hours ago
@EwaldBos in your case it will be simply equal to : if($scope.Games.findIndex((o)=>o.id_game ===value.id_game ) == -1 )
– amrender singh
22 hours ago
nice! works perfect. Just a question, could you perhaps explain how this part works, ... (o) => o.id_game ... and perhaps add it to the answer then i could select is as the right one :)
– Ewald Bos
21 hours ago
@EwaldBos updated the answer :)
– amrender singh
11 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of How to get the difference between two arrays in Javascript?
– Pop-A-Stash
21 hours ago