Is it permissible to use JavaScript's find()
function on an AngularJS array? I am encountering some issues with this straightforward code snippet. The error message claims that the return value from $scope.names.find(name1)
is not a function.
TypeError: Name1 is not a function
if($scope.names.find(name1) !== name1) {
$scope.names.push(name1);
}
I have attempted alternative solutions...
if($scope.names.find(name1) === undefined) {
$scope.names.push(name1);
}
and
if(!$scope.names.find(name1)) {
$scope.names.push(name1);
}
This conditional statement resides within a loop. It adds 'name' to the array if it doesn't already exist, and skips adding it if it does.
The loop looks like this:
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);
}
});
The specific cause of the error eludes me. There must be a misuse of find()
.