Array.find (value) return value 'not a function'

I am trying to use a JavaScript function find()in an AngularJS array. Is it legal, right ...?

This very simple code causes some problems. He says the return value from is $scope.names.find(name1)not a function.

TypeError: Name1 is not a function

if($scope.names.find(name1) !== name1) {
   $scope.names.push(name1);
}

I also tried ...

if($scope.names.find(name1) === undefined) {
   $scope.names.push(name1);
}

and

if(!$scope.names.find(name1)) {
   $scope.names.push(name1);
}

This one ifis in a loop. If namenot in the array, add it. If it is already in the array, do not add it.

Here is the loop:

angular.forEach($scope.names1, function (name1) {
    angular.forEach($scope.names2, function (name2) {
        if (name1 === name2) {
            $scope.names.push(name1);
        }
    });
    if ($scope.names.find(name1) === name1) {
        $scope.names.push(name1);
    }
});

I don’t know what exactly the error relates to. I have to abuse it find().

+4
source share
2 answers

indexOf.

if($scope.names.indexOf(name1) < 0) { //Value not exists in your array
   $scope.names.push(name1);
}
+3

underscore.js , _.find()

0

Source: https://habr.com/ru/post/1610615/


All Articles