I am attempting to use underscore to compare two objects, specifically focusing on comparing the key/value pairs of "id
" (as other elements within may change). My goal is to identify if object A contains an item that object B does not have, and then remove it from object A. Below is my current approach:
for(var c=0;c<$scope.types.length;c++){
var real = _.some($scope.storeTempName, function(it) {
return it.id == $scope.types[c].typeId;
});
if(real){
}else{
$scope.types.splice(c,1);
}
}
In this code snippet, $scope.storeTempName
represents object B and $scope.types
represents object A. Therefore, if $scope.types
includes something that is not present in $scope.storeTempName
, it should be removed (matching by id and typeId values for types).
While this initial method works, it only removes the first occurrence. I suspect this could be due to iterating from index 0 onwards and the changing indices when removing the first item. I am uncertain about this observation and would appreciate some guidance. Thank you for your assistance!