admin管理员组文章数量:1302870
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()
.
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()
.
-
4
As you can see in Docs
find
takes callback function as argument, useif (!$scope.names.find(function (name1) { $scope.names.push(name1); }) }
– Tushar Commented Oct 7, 2015 at 16:08 -
No, it's saying
name1
isn't a function. – Dave Newton Commented Oct 7, 2015 at 16:09 -
TypeError: name1 is not a function
because.find
takes a callback function and executes it on array items, and returns the value of an item that satisfies the callback function. developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – pawel Commented Oct 7, 2015 at 16:10 - 1 @DaveNewton The error is self-explanatory, it says that the passed parameter is not a function, that means parameter should be a function – Tushar Commented Oct 7, 2015 at 16:10
- @Tushar Which is precisely what I said. – Dave Newton Commented Oct 7, 2015 at 16:17
2 Answers
Reset to default 5You need to use indexOf
. Your snippet would be
if($scope.names.indexOf(name1) < 0) { //Value not exists in your array
$scope.names.push(name1);
}
Alternatively, consider using underscore.js There are a lot of useful functions including _.find()
本文标签: javascriptArrayfind(value) return value 39is not a function39Stack Overflow
版权声明:本文标题:javascript - Array.find(value) return value 'is not a function' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741689104a2392615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论