uniapp-X 对象动态取值

有个对象,例如

复制代码
const data={
						age:12,
						list:[
							1,2,3,4
						]
					}

有个函数如下

复制代码
export function getValueByPath(obj:UTSJSONObject, path:string):any {
  const current= obj.getAny(path) as any;
    // 返回最终的值
    return current;
}

期待

通过执行getValueByPath("xx.xx")来访问内部的值,不管存不存在,不管返回类型

实现

复制代码
//一定要as any,不能as unkonw,编译成android会报错,
//开发工具:web提示是as unkonw再 as UTSJSONObject
//记得不要这样!!!!!!,一定要as any

const obj=this.obj as any  as UTSJSONObject;
this.readData=JSON.stringify(getValueByPath(obj,this.path));
try{
	const realData=JSON.parse(this.readData)
//取出来后再判断类型即可
	console.log("typeof",typeof realData,realData instanceof Array)
}catch(err){
	console.error("err",err)
}
相关推荐
2501_915921431 天前
Fastlane 结合 AppUploader 来实现 CI 集成自动化上架
android·运维·ci/cd·小程序·uni-app·自动化·iphone
云游云记1 天前
vue2 vue3 uniapp (微信小程序) v-model双向绑定
微信小程序·uni-app·vue
2501_915921431 天前
iOS 抓包怎么绕过 SSL Pinning 证书限制,抓取app上的包
android·网络协议·ios·小程序·uni-app·iphone·ssl
予你@。2 天前
uni-app(Vue3)实现自定义 Tab 切换滑块效果(微信小程序)
vue.js·微信小程序·uni-app
HashTang3 天前
【AI 编程实战】第 11 篇:让小程序飞起来 - 性能优化实战指南
前端·uni-app·ai编程
lruri3 天前
记录一个修复nvue文件在vscode里面提示ts-plugin报错
uni-app
蓝帆傲亦3 天前
Web前端Mock数据实战指南:正确使用Mock.js提升开发效率
微信小程序·小程序·uni-app
00后程序员张3 天前
iOS 应用代码混淆,对已编译 IPA 进行类与方法混淆
android·ios·小程序·https·uni-app·iphone·webview
木子啊3 天前
Uni-app社会化功能:登录支付分享全攻略
uni-app
笨笨狗吞噬者4 天前
【2025】加入 uniapp 的一年
前端·uni-app