gpt4 book ai didi

javascript - Javascript array.indexOf 不起作用

转载 作者:行者123 更新时间:2023-12-02 14:29:42 26 4
gpt4 key购买 nike

这很令人惊讶,但简单的函数 array.IndexOf 不起作用。

$scope.nextProduct = function (pos, item) {
switch (pos) {
case 0: product = $scope.Menu[0].Breakfast
break
case 1: product = $scope.Menu[0].Lunch
break
case 2: product = $scope.Menu[0].BeforTraining
break
case 3: product = $scope.Menu[0].AfterTraining
break
case 4: product = $scope.Menu[0].Dinner
break
default: product = $scope.Menu[0].Breakfast
break
}
var index = product.indexOf(item.Name);
product[index - 1].IsSelect = false;
product[index + 1].IsSelected = true;
}

indexOf 返回 -1 但我完全确定该项目存在于数组中。这里可能出了什么问题? enter image description here

最佳答案

通过此表达式,您将在对象数组中搜索字符串。

product.indexOf(item.Name);

相反,你应该运行:

var res = product.filter(function(elem){
return elem.Name == someValue
})

这将返回与您的值匹配的数组

关于javascript - Javascript array.indexOf 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37945839/

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