gpt4 book ai didi

javascript - charAt 返回为未定义

转载 作者:行者123 更新时间:2023-11-29 16:11:09 26 4
gpt4 key购买 nike

我有以下函数,但它说 charAt 未定义。该错误与警报行有关。如果我执行 alert(value) 它给我的值没有问题。

$scope.markAnswer = function(answerID, questionID) {
if ($scope.containsObject(answerID, $scope.selectedAnswer)) {
$scope.selectedAnswer.splice($scope.selectedAnswer.indexOf(answerID), 1);
} else {
$scope.selectedAnswer.push(answerID);
}

angular.forEach($scope.selectedAnswer, function(value, key) {
alert(value.charAt(0));
if(questionID == res){
$log.info("questionID");
}

});
}

以下错误:

 TypeError: undefined is not a function
at http://127.0.0.1:9000/modules/core/controllers/home.js:57:25
at Object.forEach (http://127.0.0.1:9000/lib/angular/angular.js:325:18)
at Scope.$scope.markAnswer (http://127.0.0.1:9000/modules/core/controllers/home.js:56:17)
at http://127.0.0.1:9000/lib/angular/angular.js:10903:21
at http://127.0.0.1:9000/lib/angular-touch/angular-touch.js:441:9
at Scope.$eval (http://127.0.0.1:9000/lib/angular/angular.js:12811:28)
at Scope.$apply (http://127.0.0.1:9000/lib/angular/angular.js:12909:23)
at HTMLDivElement.<anonymous> (http://127.0.0.1:9000/lib/angular-touch/angular-touch.js:440:13)
at HTMLDivElement.jQuery.event.dispatch (http://127.0.0.1:9000/lib/jquery/dist/jquery.js:4430:9)
at HTMLDivElement.elemData.handle (http://127.0.0.1:9000/lib/jquery/dist/jquery.js:4116:28)

最佳答案

你可以试试这个:

String(value).charAt(0)

我想这是因为您的值不是字符串,而 charAt 是一种返回 string 中指定索引处字符的方法。

关于javascript - charAt 返回为未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27777841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com