Skip to content

Instantly share code, notes, and snippets.

@ThomasKruegl
Created February 3, 2021 17:42
Show Gist options
  • Save ThomasKruegl/080a04d53d308514d706317c81b3d15a to your computer and use it in GitHub Desktop.
Save ThomasKruegl/080a04d53d308514d706317c81b3d15a to your computer and use it in GitHub Desktop.
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