1

I have this array $scope.userEventData.selectedFlats and there's an other array $scope.flatsArray

I want to remove values from $scope.userEventData.selectedFlats that are present in $scope.flatsArray.

I have done this:

$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
    return !$scope.someObj.flatsArray.some(function(v){
                return v.indexOf(f) >= 0;
    })
})

But I get an error saying v.indexOf is not a function

4 Answers4

1

The v in flatsArray.some callback function returns a single item rather than an array of items. So rather than checking for the index of, you can simply compare the values directly.

You need to

$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
    return !$scope.someObj.flatsArray.some(function(v){
                return v == f;
    })
})
nipuna-g
  • 6,252
  • 3
  • 31
  • 48
0

Pick either some or indexOf. For example

$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
    return $scope.someObj.flatsArray.indexOf(f) === -1
})

or

$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
    return !$scope.someObj.flatsArray.some(function(item) { return item === f; })
})
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
0

It might be helpful to post the arrays so they can be used in answers, however, you could do this.

for (var i = 0; i < $scope.userEventData.selectedFlats; i++) {
    var index = $scope.flatsArray.indexOf($scope.userEventData.selectedFlats[i]);
    if ( index > -1 ) {
        $scope.userEventData.selectedFlats.splice(index, 1);
    } 
}

This will loop through each item in the selectFlats array and find the index of that item in the flatsArray, then remove that item from if it exists.

0

Try this:

$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(
    function(item) {
        return !($scope.someObj.flatsArray.contains(item))
    }
Jorgo
  • 81
  • 3