CDN
CDN

Reputation: 410

Handling an array of object with variable structure

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

Upvotes: 2

Views: 110

Answers (1)

MUHAMMAD ILYAS
MUHAMMAD ILYAS

Reputation: 1450

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));

Upvotes: 1

Related Questions