I'm trying to use JavaScript's find() function on an AngularJS array. That's legal, right...?
This very simple code is causing me some problems. It's saying 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 also tried...
if($scope.names.find(name1) === undefined) {
$scope.names.push(name1);
}
and
if(!$scope.names.find(name1)) {
$scope.names.push(name1);
}
This if is in a loop. If name is not in the array, then add it. If it is already in the array, don't 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 the error is referring to exactly. I must be misusing find().
findtakes callback function as argument, useif (!$scope.names.find(function (name1) { $scope.names.push(name1); }) }name1isn't a function.TypeError: name1 is not a functionbecause.findtakes a callback function and executes it on array items, and returns the value of an item that satisfies the callback function. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…