Created
May 5, 2021 15:36
-
-
Save bloo/6115925124cba6ad3a502eebdbebeb3e to your computer and use it in GitHub Desktop.
Resilient Tech Blog - Building Forms with Formstack and React - foreach.ts
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
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