You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS调用数组findIndex方法报错Uncaught TypeError: Lemon is not a function如何解决

报错原因

Array.prototype.findIndex() 方法的入参要求为回调函数,你直接传入字符串"Lemon"作为参数,JS运行时会尝试将传入的字符串作为函数执行,等价于调用"Lemon"(),因此触发Lemon is not a function类型错误。该报错和数组中是否存在Lemon值无关,属于方法入参类型不符合要求导致的前置错误。

解决方案

根据你的使用场景,可以选择以下两种修正方式:

  • 方式1:给findIndex传入符合要求的回调函数,适用于需要自定义匹配规则的场景
var guildList = [];
guildList.push("Lemon");
console.log(guildList.findIndex(item => item === "Lemon")); // 输出0,匹配到返回对应下标,未匹配到返回-1
  • 方式2:直接使用Array.prototype.indexOf()方法,该方法支持直接传入要匹配的目标值,更适合简单全等匹配的场景
var guildList = [];
guildList.push("Lemon");
console.log(guildList.indexOf("Lemon")); // 输出0,匹配到返回对应下标,未匹配到返回-1

内容的提问来源于stack exchange,提问作者Osian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 22:15:01