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 小时前
uniapp实现生成海报功能 (开箱即用)
javascript·vue.js·小程序·uni-app
笨笨狗吞噬者3 小时前
【uniapp】小程序支持分包引用分包 node_modules 依赖产物打包到分包中
前端·微信小程序·uni-app
har01d7 小时前
uni-app 自动更新检测弹窗,踩坑记录
uni-app
Muchen灬10 小时前
【uniapp】(3)引入uviewUI组件
uni-app
毕设源码-朱学姐1 天前
【开题答辩全过程】以 基于uniapp的云笔记系统的设计与实现为例,包含答辩的问题和答案
笔记·uni-app
游戏开发爱好者81 天前
如何使用Instruments和Keymob进行Swift应用性能优化分析
开发语言·ios·性能优化·小程序·uni-app·iphone·swift
外派叙利亚1 天前
uniapp 颜色卡条拖动
前端·javascript·uni-app
Muchen灬1 天前
【uniapp】(2) uni-ui组件引入
uni-app
NPCZ1 天前
uniapp更新到最新版5.03后报错的解决方案
uni-app
2501_916008891 天前
移动应用上架到应用商店的完整指南:原理与详细步骤
android·ios·小程序·https·uni-app·iphone·webview