Skip to content

Commit a928619

Browse files
chore
1 parent 57985f0 commit a928619

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

packages/form-core/src/utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,22 @@ export function setBy(obj: any, _path: any, updater: Updater<any>) {
9696
*/
9797
export function deleteBy(obj: any, _path: any) {
9898
const path = makePathArray(_path)
99+
const lastPathIndex = path.length - 1
100+
let pathIndex = 0
99101

100102
function doDelete(parent: any): any {
101103
if (!parent) return
102-
if (path.length === 1) {
103-
const finalPath = path[0]!
104+
if (pathIndex === lastPathIndex) {
105+
const finalPath = path[lastPathIndex]!
104106
if (Array.isArray(parent) && typeof finalPath === 'number') {
105107
return parent.filter((_, i) => i !== finalPath)
106108
}
107109
const { [finalPath]: remove, ...rest } = parent
108110
return rest
109111
}
110112

111-
const key = path.shift()
113+
const key = path[pathIndex]!
114+
pathIndex++
112115

113116
if (typeof key === 'string') {
114117
if (typeof parent === 'object') {
@@ -126,7 +129,7 @@ export function deleteBy(obj: any, _path: any) {
126129
}
127130
const prefix = parent.slice(0, key)
128131
return [
129-
...(prefix.length ? prefix : new Array(key)),
132+
...(prefix.length ? prefix : Array.from({ length: key })),
130133
doDelete(parent[key]),
131134
...parent.slice(key + 1),
132135
]

0 commit comments

Comments
 (0)