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)
}
相关推荐
街尾杂货店&2 小时前
word属性解释
uni-app
笨笨狗吞噬者12 小时前
【uniapp】小程序体积优化,JSON文件压缩
前端·微信小程序·uni-app
源码集结号14 小时前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
木易 士心20 小时前
Uni-App 实现多身份动态切换 TabBar 指南
uni-app
2501_915106322 天前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone
LoveEate2 天前
uniapp 运行/发版微信小程序
微信小程序·小程序·uni-app
fakaifa3 天前
【高级版】沃德政务招商系统源码+uniapp小程序
小程序·uni-app·源码下载·沃德政务招商系统·招商系统源码
weixin_446938873 天前
uniapp vue-i18n如何使用
前端·vue.js·uni-app
有来技术3 天前
UniApp 自定义导航栏适配指南:微信小程序胶囊遮挡、H5 与 App 全端通用方案
微信小程序·uni-app
卷Java3 天前
违规通知功能修改说明
java·数据库·微信小程序·uni-app