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

出现TypeError: sportsRecord.find is not a function错误,附sportsRecord数据求排查

排查TypeError: sportsRecord.find is not a function错误

嘿,这个问题我太熟悉了!咱们直接拆解问题:

错误根本原因

你在一个普通JavaScript对象上调用了只有数组才有的find()方法!从你贴的控制台输出能看到,sportsRecord的结构是这样的:

{ playerTigers: [ {TigerNo: 237, ...}, {TigerkNo: 238, ...}, ... ] }

sportsRecord本身是一个包含playerTigers属性的对象,而find()是Array.prototype上的专属方法——对象没有这个方法,所以直接调用sportsRecord.find()必然报错。

解决方案

根据你的业务需求,分两种情况处理:

1. 目标是查找playerTigers数组里的某条记录(最常见场景)

直接对sportsRecord.playerTigers这个数组调用find()就行,比如你要找TigerNo为237的记录:

// 示例:查找指定TigerNo的记录
const targetPlayer = sportsRecord.playerTigers.find(item => item.TigerNo === 237);

⚠️ 小提醒:你贴的数据里字段名有拼写不一致的情况(比如有的是TigerNo,有的是TigerkNo),如果是笔误记得统一,不然查找会匹配不到结果哦。

2. 目标是遍历sportsRecord对象的属性(少见场景)

如果你确实需要从sportsRecord对象的所有属性值里查找内容,得先把对象的属性转化为数组,再用find(),比如:

// 查找对象中包含特定老虎记录的数组属性
const targetArrayProp = Object.values(sportsRecord).find(value => 
  Array.isArray(value) && value.some(item => item.TigerName.includes("Bird"))
);

额外防坑小技巧

为了避免类似类型错误,建议调用数组方法前先做类型校验:

if (Array.isArray(sportsRecord.playerTigers)) {
  const result = sportsRecord.playerTigers.find(item => item.isDefault);
} else {
  console.error('playerTigers不是数组,请检查数据结构!');
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:52:36