Created
February 3, 2021 17:42
-
-
Save ThomasKruegl/080a04d53d308514d706317c81b3d15a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getFields(form) { | |
if (typeof form !== "object") { | |
return []; | |
} else { | |
if (form.formType === "formField") { | |
// end case, object is a simple field | |
return [{htmlName: form.htmlName, value: form.value}]; | |
} else if (form.formType === "formGroup") { | |
// recursion: check every entry of the formGroup | |
return Object.entries(form) | |
.filter(([key, value]) => typeof value === "object") | |
.flatMap(([key, value]) => getFields(value)); | |
} else { | |
// probably not necessary, depending on other options for formType | |
return []; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment