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)
}
相关推荐
软希网分享源码15 小时前
中英双语言量化交易投资源码/跟单搬砖区块链交易所源码/前端uniapp纯源码+后端
前端·uni-app·区块链·中英双语言量化交易投资源码
小成Coder15 小时前
UniApp 如何调用鸿蒙预加载
uni-app·harmonyos·鸿蒙
anyup1 天前
uView Pro 的主题系统有多强大?3 分钟设计 uni-app 企业级 UI 主题
前端·vue.js·uni-app
怀君2 天前
Uniapp——View布局生成图片转PDF
pdf·uni-app
2501_915921432 天前
uni-app一键生成iOS安装包并上传TestFlight全流程
android·ios·小程序·https·uni-app·iphone·webview
久爱@勿忘2 天前
uniapp H5 图片压缩并且转blob
前端·javascript·uni-app
Dashingl2 天前
uni-app 页面传值 报错:TypeError: $t.setAttribute is not a function
前端·javascript·uni-app
橘子编程3 天前
UniApp跨端开发终极指南
开发语言·vue.js·uni-app
叱咤少帅(少帅)5 天前
Uniapp开发pc端,小程序和APK
小程序·uni-app