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 if
is in a loop. If name
not 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()
.
source
share