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_9160074724 分钟前
React Native 混淆在真项目中的方式,当 JS 和原生同时暴露
javascript·react native·react.js·ios·小程序·uni-app·iphone
00后程序员张1 小时前
苹果应用商店上架App流程,签名证书、IPA 校验、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 小时前
iOS 上架需要哪些准备,围绕证书、描述文件和上传方式等关键环节展开分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 小时前
iOS 上架费用解析,哪些成本可以通过流程优化降低。
android·ios·小程序·https·uni-app·iphone·webview
小离a_a3 小时前
uniapp微信小程序实现拍照加水印,水印上添加当前时间,当前地点等信息,地点逆解析使用的是高德地图
微信小程序·小程序·uni-app
前端小雪的博客.4 小时前
uniapp小程序顶部状态栏占位和自定义头部导航栏
小程序·uni-app
2501_916007471 天前
iOS 证书如何创建,从能生成到能长期使用
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张1 天前
AppStoreInfo.plist 在苹果上架流程中的生成方式和作用
android·小程序·https·uni-app·iphone·webview
2501_915106321 天前
iOS App 测试方法,通过 Xcode、Instruments、Safari Inspector、克魔(KeyMob)等工具
android·ios·小程序·uni-app·iphone·xcode·safari
游戏开发爱好者81 天前
对 iOS IPA 文件进行深度混淆的一种实现路径
android·ios·小程序·https·uni-app·iphone·webview