mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-03 03:38:41 +08:00
feat(utils): getPropByPath function adjust (#1276)
Co-authored-by: 三咲智子 <sxzz@sxzz.moe>
This commit is contained in:
parent
00d38e1807
commit
4d3a499fa6
@ -49,28 +49,37 @@ export function getPropByPath(
|
||||
v: Nullable<unknown>
|
||||
} {
|
||||
let tempObj = obj
|
||||
path = path.replace(/\[(\w+)\]/g, '.$1')
|
||||
path = path.replace(/^\./, '')
|
||||
let key, value
|
||||
|
||||
const keyArr = path.split('.')
|
||||
let i = 0
|
||||
for (i; i < keyArr.length - 1; i++) {
|
||||
if (!tempObj && !strict) break
|
||||
const key = keyArr[i]
|
||||
if (obj && hasOwn(obj, path)) {
|
||||
key = path
|
||||
value = tempObj?.[path]
|
||||
} else {
|
||||
path = path.replace(/\[(\w+)\]/g, '.$1')
|
||||
path = path.replace(/^\./, '')
|
||||
|
||||
if (key in tempObj) {
|
||||
tempObj = tempObj[key]
|
||||
} else {
|
||||
if (strict) {
|
||||
throw new Error('please transfer a valid prop path to form item!')
|
||||
const keyArr = path.split('.')
|
||||
let i = 0
|
||||
for (i; i < keyArr.length - 1; i++) {
|
||||
if (!tempObj && !strict) break
|
||||
const key = keyArr[i]
|
||||
|
||||
if (key in tempObj) {
|
||||
tempObj = tempObj[key]
|
||||
} else {
|
||||
if (strict) {
|
||||
throw new Error('please transfer a valid prop path to form item!')
|
||||
}
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
key = keyArr[i]
|
||||
value = tempObj?.[keyArr[i]]
|
||||
}
|
||||
return {
|
||||
o: tempObj,
|
||||
k: keyArr[i],
|
||||
v: tempObj?.[keyArr[i]],
|
||||
k: key,
|
||||
v: value,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user