Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mappedData = this.state.treeData
- .filter(data => data.title === "Category")
- .map(categ => {
- const { id, title, children, subtitle, type } = categ;
- function getChildren(children) {
- return children
- ? children.map(child => {
- if (child.title === "Item" || child.title === "Group") {
- const data = {
- id: child.id,
- name: child.subtitle,
- type: child.type,
- children: getChildren(child.children),
- child_id: child.children
- ? child.children.map(child => child.id)
- : []
- };
- if (data.children.length === 0) delete data.children;
- if (data.child_id.length === 0) delete data.child_id;
- return data;
- } else {
- return {
- id: child.id,
- name: child.subtitle,
- type: child.type
- };
- }
- })
- : [];
- }
- const data = {
- id: id,
- name: subtitle,
- type: type,
- children: getChildren(children),
- child_id: children ? children.map(child => child.id) : []
- };
- if (data.children.length === 0) delete data.children;
- if (data.child_id.length === 0) delete data.child_id;
- return data;
- });
- <div className="json">
- <p> {JSON.stringify(mappedData)}</p>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement