school-dashboard-exercise/src/Components/ValidationField/utils/getNestedValue.ts
karimalden b873ac0bba format
2024-08-07 14:52:29 +03:00

8 lines
319 B
TypeScript

export function getNestedValue(obj: any, path: any) {
return path
.replace(/\?.\[|\]\[|\]\.?/g, ".") // Replace question mark and square brackets
.split(".") // Split by dots
.filter(Boolean) // Remove empty strings
.reduce((acc: any, key: any) => acc && acc[key], obj); // Access nested properties
}