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_9151063233 分钟前
当 Perfdog 开始收费之后,我重新整理了一替代方案
android·ios·小程序·https·uni-app·iphone·webview
2501_915918413 小时前
中小团队发布,跨平台 iOS 上架,证书、描述文件创建管理,测试分发一体化方案
android·ios·小程序·https·uni-app·iphone·webview
家里有只小肥猫4 小时前
uniApp打包ios报错
ios·uni-app
jingling5555 小时前
uniapp | 基于高德地图实现位置选择功能(安卓端)
android·前端·javascript·uni-app
某公司摸鱼前端5 小时前
前端一键部署网站至服务器FTP
前端·javascript·uni-app
爱怪笑的小杰杰5 小时前
UniApp 桌面应用实现 Android 开机自启动(无原生插件版)
android·java·uni-app
m0_647057965 小时前
uniapp使用rich-text流式 Markdown 换行问题与解决方案
前端·javascript·uni-app
木子啊6 小时前
Uni-app导航栏适配终极避坑指南
uni-app·自定义导航栏·导航栏
2501_915106326 小时前
iOS 如何绕过 ATS 发送请求,iOS调试
android·ios·小程序·https·uni-app·iphone·webview
行走的陀螺仪17 小时前
uni-app + Vue3编辑页/新增页面给列表页传参
前端·vue.js·uni-app