gpt4 book ai didi

javascript - JavaScript 数组上的 indexOf 问题

转载 作者:行者123 更新时间:2023-12-03 23:33:58 26 4
gpt4 key购买 nike

var allProductIDs = [5410, 8362, 6638, 6758, 7795, 5775, 1004, 1008, 1013, 1014, 1015, 1072, 1076, 1086, 1111, 1112, 1140];

lastProductID = 6758;

出于某种原因,我得到了一个 -1 或者我猜这相当于没有找到:

alert(allProductIDs[allProductIDs.indexOf(lastProductID));

我一辈子都想不通为什么,因为它应该找到 6758,那将是索引 3。如果它是索引 3,那么我认为我应该找回 6758。

最佳答案

.indexOf() 用于字符串,而不是数组。

使用常规 Javascript,您必须遍历数组直到找到匹配项,或者使用 jQuery 的 inArray() 函数。

jQuery inArray()

关于javascript - JavaScript 数组上的 indexOf 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1071257/

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