2

I have an array of object that defines file and directory structure. It will vary according to reality, which may be more complicated.

Here is an example :

For example this root folder contains "folder_id": "F1" and "folder_id": "F2", and it will continue to divide ...

const folders = [
    {
      folder_id: "F1",
      name: "Test Folder 1",
      files: [
        {
          file_id: "File1",
          name: "Whiteboard-Jun 3rd 2020, 4:56 pm"
        }
      ],
      folders: [
        {
          folder_id: "Folder1",
          name: "Sub Folder",
          files: [
            {
              file_id: "File1-1",
              name: "New Microsoft Word Document.docx"
            }
          ],
          folders: [
            {
              folder_id: "Folder1-1",
              name: "Folder Grade 2",
              files: [
                {
                  file_id: "File1-1-1",
                  name: "Test file in Folder Grade 3"
                }
              ],
              folders: [
                {
                  folder_id: "Folder1-1-1",
                  name: "Folder Grade 3",
                  files: [],
                  folders: []
                }
              ]
            }
          ]
        }
      ]
    },
    {
      folder_id: "F2",
      name: "Test Folder 2",
      files: [
        {
          file_id: "File2",
          name: "buildcode.png"
        }
      ],
      folders: [
        {
          folder_id: "Folder2",
          name: "Sub folder 1",
          files: [
            {
              file_id: "File2-1",
              name: "repo.png"
            }
          ],
          folders: []
        }
      ]
    }
  ];

I want the output to look like this:

{
	my-root-id:{
		childrenIds:[
			0: "F1",
			1: "F2"
		],
		id: "my-root-id"
	},
	F1:{
		childrenIds:[
			0: "File1",
			1: "Folder1"
		],
		parentId: "my-root-id",
		id: "F1"
	},
	F2:{
		childrenIds:[
			0: "File2",
			1: "Folder2"
		],
		parentId: "my-root-id",
		id: "F2"
	},
	File1:{
		parentId: "File1",
		id: "F1"
	},
	Folder1:{
		childrenIds:[
			0: "File1-1",
			1: "Folder1-1"
		],
		parentId: "F1",
		id: "Folder1"
	},
	Folder1-1:{
		childrenIds:[
			0: "File1-1-1",
			1: "Folder1-1-1"
		],
		parentId: "Folder1",
		id: "Folder1-1"
	},
	Folder2:{
		childrenIds:[
			0: "File2-1",
		],
		parentId: "F2",
		id: "Folder2"
	},
	File2-1:{
		parentId: "Folder2",
		id: "File2-1"
	}
	
}	

The output above describes the relationship of each folder and file in the entire directory tree.

They need information like :

childrenIds: the folders and files it contains

parentId: Its parent directory

id: id by itself

How to do it. thank you

1
  • 1
    Try it using recursion. Commented Jun 14, 2020 at 13:00

1 Answer 1

1

Don't know this actually works for you or not but output as you look for

let data = {
  folders: [
    {
      folder_id: "F1",
      name: "Test Folder 1",
      files: [
        {
          file_id: "File1",
          name: "Whiteboard-Jun 3rd 2020, 4:56 pm",
        },
      ],
      folders: [
        {
          folder_id: "Folder1",
          name: "Sub Folder",
          files: [
            {
              file_id: "File1-1",
              name: "New Microsoft Word Document.docx",
            },
          ],
          folders: [
            {
              folder_id: "Folder1-1",
              name: "Folder Grade 2",
              files: [
                {
                  file_id: "File1-1-1",
                  name: "Test file in Folder Grade 3",
                },
              ],
              folders: [
                {
                  folder_id: "Folder1-1-1",
                  name: "Folder Grade 3",
                  files: [],
                  folders: [],
                },
              ],
            },
          ],
        },
      ],
    },
    {
      folder_id: "F2",
      name: "Test Folder 2",
      files: [
        {
          file_id: "File2",
          name: "buildcode.png",
        },
      ],
      folders: [
        {
          folder_id: "Folder2",
          name: "Sub folder 1",
          files: [
            {
              file_id: "File2-1",
              name: "repo.png",
            },
          ],
          folders: [],
        },
      ],
    },
  ],
};

const mapper = (folders, parentId) => {
  let obj = {};
  if (folders.length) {
    folders.forEach((f) => {
      if (f.folders) {
        let _c = {
          [f.folder_id]: {
            childrenIds: f.files.map((_f) => _f.file_id),
            ...(parentId && { parentId }),
            id: f.folder_id,
          },
        };
        let _files = f.files.map((_f) => {
          return {
            [_f.file_id]: { ...(parentId && { parentId }), id: _f.file_id },
          };
        });
        obj = { ...obj, ..._files["0"], ..._c, ...mapper(f.folders, f.folder_id) };
      }
    });
  }
  return obj;
};

console.log(mapper(data.folders));

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks you . @MUHAMMAD ILYAS , but it seems the output is not correct
is outer keys are not matching?
ooh! you are right files key is not mapped like others.
@123456 answer updated and now you can manipulate as your need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.