Skip to content

Commit 6aa2623

Browse files
committed
fix: improve getNestedField to fetch field in objects containing arrays
- the regex breaks the path into segments, handling both dot (.) and bracket ([]) notations - the filter(Boolean) ensures empty strings are excluded Signed-off-by: Dave Canton <[email protected]>
1 parent 54f36c7 commit 6aa2623

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/components/form-field/form-field.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import { v1 as uuid } from 'uuid';
99
require('./form-field.scss');
1010

1111
export function getNestedField(src: any, path: string): any {
12-
const parts = path.split('.');
12+
const parts = path.split(/\.|\[|\]/).filter(Boolean);
1313
while (parts.length > 0 && src) {
14-
src = src[parts.splice(0, 1)[0]];
14+
let segment = parts.splice(0, 1)[0]
15+
src = src[isNaN(segment as any) ? segment : Number(segment)];
1516
}
1617
return src;
1718
}

0 commit comments

Comments
 (0)