Skip to content

Commit

Permalink
fix: improve getNestedField to fetch field in objects containing arrays
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
dvcanton committed Nov 28, 2024
1 parent 54f36c7 commit 6aa2623
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/form-field/form-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { v1 as uuid } from 'uuid';
require('./form-field.scss');

export function getNestedField(src: any, path: string): any {
const parts = path.split('.');
const parts = path.split(/\.|\[|\]/).filter(Boolean);
while (parts.length > 0 && src) {
src = src[parts.splice(0, 1)[0]];
let segment = parts.splice(0, 1)[0]
src = src[isNaN(segment as any) ? segment : Number(segment)];
}
return src;
}
Expand Down

0 comments on commit 6aa2623

Please sign in to comment.