Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bloo/6115925124cba6ad3a502eebdbebeb3e to your computer and use it in GitHub Desktop.
Save bloo/6115925124cba6ad3a502eebdbebeb3e to your computer and use it in GitHub Desktop.
Resilient Tech Blog - Building Forms with Formstack and React - foreach.ts
export const nestFieldsInSections = (fields: FieldBase[]): FieldBase[] => {
const sections: FieldBase[] = []
fields.forEach((field) => {
if (field.type === 'section') {
field.subfields = []
sections.push(field)
} else if (field.type === 'embed') {
const metadata: Embed = JSON.parse(field.section_text)
sections[sections.length - 1].metadata = metadata
} else {
sections[sections.length - 1].subfields?.push(field)
}
})
return sections
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment