vendredi 5 décembre 2014

Check if item from one object exists in another (using underscore)


Vote count:

0




I am trying to compare 2 objets using underscore, specifically I am trying to compare the key/values of "id" (because other things inside will change). I basically want to just check if object A has an item that object B does not have, remove it from object A. Here is my attempt at it :



for(var c=0;c<$scope.promptsPlease.currentData.selections[$scope.typeTracker].types.length;c++){
var real = _.some($scope.storeTempName, function(it) {
return it.id == $scope.promptsPlease.currentData.selections[$scope.typeTracker].types[c].typeId;
});

if(real){
}else{
$scope.promptsPlease.currentData.selections[$scope.typeTracker].types.splice(,1);
}

}


Where $scope.storeTempName is object B and $scope.promptsPlease.currentData.selections[$scope.typeTracker].types is object A. So if $scope.promptsPlease.currentData.selections[$scope.typeTracker].types has something $scope.storeTempName does not, remove it (tracking by id and typyId for types).


This first attempt I have works, BUT it only will remove the first one. My guess is it's becasue I'm looping from 0 ++ and the index's are changing when i remove the first one so splice is targetting a false item. I am not sure though, and could use some help. Thank you for reading!



asked 44 secs ago







Check if item from one object exists in another (using underscore)

Aucun commentaire:

Enregistrer un commentaire