uni-app find()方法内不能查找变量(变量已通过this.barcode赋值)

uni-app find()方法内不能查找变量(变量已通过this.barcode赋值)

开发环境 版本号 项目创建方式
Windows win7 旗舰版 HBuilderX
产品分类:uniapp/App

PC开发环境操作系统:Windows

HBuilderX类型:正式

HBuilderX版本号:3.1.17

手机系统:Android

手机系统版本号:Android 10

手机厂商:华为

手机机型:荣耀9x

页面类型:vue

打包方式:云端

示例代码:
var barcode=this.barcode    //this.barcode已通过this.barcode赋值console.log可输出值
var inde = gett.find(o =>o.fTM == barcode)
console.log(inde)
提示:
undefined  
操作步骤:
var barcode=this.barcode    //this.barcode已赋值console.log可输出值
var inde = gett.find(o =>o.fTM == barcode)
console.log(inde)
预期结果:
获得一个与this.barcode相关的对象
实际结果:
提示:
undefined
bug描述:
find()方法内不能查找变量(变量已通过this.barcode赋值)
var barcode=this.barcode    //this.barcode已通过this.barcode赋值console.log可输出值
var inde = gett.find(o =>o.fTM == barcode)
console.log(inde)

更多关于uni-app find()方法内不能查找变量(变量已通过this.barcode赋值)的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app find()方法内不能查找变量(变量已通过this.barcode赋值)的实战教程也可以访问 https://www.itying.com/category-93-b0.html


问题可能出在数据类型不匹配或数组查找逻辑上。find()方法返回undefined说明没有找到匹配项,建议按以下步骤排查:

  1. 检查gett数据结构:确认gett是数组且包含fTM字段
console.log('gett类型:', Array.isArray(gett))
console.log('gett内容:', gett)
  1. 严格比较数据类型:使用==可能导致类型隐式转换
var inde = gett.find(o => String(o.fTM) === String(barcode))
  1. 验证字段值:打印所有fTM值进行比对
console.log('所有fTM值:', gett.map(item => item.fTM))
console.log('目标barcode:', barcode, '类型:', typeof barcode)
  1. 处理空值情况:添加空值保护
var inde = gett.find(o => o.fTM != null && o.fTM.toString() === barcode.toString())
回到顶部