Ai
1 Star 2 Fork 1

旻天信息/mttk-vue-wrap

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pathUtil.ts 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
旻天信息 提交于 2024-05-14 09:26 +08:00 . Update license
import { isRef} from 'vue'
//Obtain value from data by path,return undefined if no value is found
export function getByPath(data:any,path:string){
if (!path){
return data
}
//
let temp=isRef(data)?data.value:data;
//Split path by .
let pathItems=path.split(".") ;
for(let i=0;i<pathItems.length;i++){
//
// console.log(i+'==>'+pathItems[i]+'~~~'+JSON.stringify(temp))
temp=temp[pathItems[i]]
//console.log(JSON.stringify(temp))
if(temp==undefined){
return undefined
}
}
//
return temp;
}
//Set value to data by path
//Force:Create the proper object if path is not reachable
export function setByPath(data: any, path: string, value: any,force=false) {
if (!path) {
//
return data;
}
//
//
// console.log('####'+isRef(data)+'~~~'+isReactive(data))
let temp =isRef(data)?data.value:data;
// console.log('@@@'+isRef(temp)+'~~~'+isReactive(temp))
// console.log(data)
let pathItems = path.split(".");
for (let i = 0; i < pathItems.length; i++) {
if (i == pathItems.length - 1) {
//This is final node,set value directly
// console.log('~~~~~~~~')
// console.log(temp)
// console.log(i)
// console.log(pathItems[i])
// console.log(value)
temp[pathItems[i]] = value
break;
}
if (!temp[pathItems[i]]) {
//Path item does not existed,create
temp[pathItems[i]] = {}
}else{
if (force && (typeof temp[pathItems[i]])!='object'){
//If force create,try to create or modify to object
temp[pathItems[i]] = {}
}
}
//
temp = temp[pathItems[i]];
}
//
return data;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/jamie0828/mttk-vue-wrap.git
git@gitee.com:jamie0828/mttk-vue-wrap.git
jamie0828
mttk-vue-wrap
mttk-vue-wrap
master

搜索帮助