object value matches any in array indexOf

I am currently writing a bit of a side project. This should be quite simple. I have an array of values that looks like

values = ["Snow","Rain"];

I am trying to check an object property value against all values in the array. If there is only one value it works perfect and I assume that is because indexOf only checks against a single value. I am wondering what I should be using instead?

$scope.weather = payload.data;
                var len = $scope.weather.length;
                $scope.year = {};
                for(var i = 0; i < len; i++){
                    if($scope.weather[i].Events !== null){
                      if($scope.weather[i].Events.indexOf(values) > -1){
                        if(!$scope.year[$scope.weather[i].year]){
                            $scope.year[$scope.weather[i].year] = 1;
                        }else{
                        $scope.year[$scope.weather[i].year] += 1;

                        }

                    }
                    }
                }

Upvotes: 0

Views: 94

Answers (1)

Dan D.
Dan D.

Reputation: 74655

You could use (ES5):

values.some(function(v) {
    return $scope.weather[i].Events.indexOf(v) > -1;
})

as a drop in replacement for the incorrect:

$scope.weather[i].Events.indexOf(values) > -1

Upvotes: 1

Related Questions