Skip to content

Instantly share code, notes, and snippets.

@shojibMahabub
Created August 9, 2021 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shojibMahabub/c721d8b9cbf1621fdf3d077b5a59b6c3 to your computer and use it in GitHub Desktop.
Save shojibMahabub/c721d8b9cbf1621fdf3d077b5a59b6c3 to your computer and use it in GitHub Desktop.
SDEQ321A-NodeJs-AWS-DesignDoc-MahabubElahi
// Author : Mahabub Elahi
// Email : shojibmahabub630@gmail.com
// Description : A function that will accept the list of folders and will return another list that contains the number of descendent folders each folder has.
// Run : node index.js
getSummary(Folder);
// @param Folder
// @return Array
function getSummary(Folder)
{
let summary = []
Folder.forEach(element =>
{
let current_id = element.Id
let filter = Folder.filter(e => e.Id !== element.Id && e.ParentId === element.Id)
summary.push({'Id': current_id, 'Count': filter.length})
});
return summary;
}
// folder array
const Folder = [
{
'Id': '0',
'Name': 'root',
'ParentId': '0'
},
{
'Id': '1',
'Name': 'apache2',
'ParentId': '7'
},
{
'Id': '2',
'Name': 'site-available',
'ParentId': '1'
},
{
'Id': '3',
'Name': 'sites-enabled',
'ParentId': '1'
},
{
'Id': '4',
'Name': 'ports.conf',
'ParentId': '1'
},
{
'Id': '5',
'Name': 'google-drive-service.conf',
'ParentId': '2'
},
{
'Id': '6',
'Name': 'drop-box-service.conf',
'ParentId': '3'
},
{
'Id': '7',
'Name': 'etc',
'ParentId': '0'
},
{
'Id': '8',
'Name': 'var',
'ParentId': '0'
},
]
@shojibMahabub
Copy link
Author

Output:
[
{ Id: '0', Count: 2 },
{ Id: '1', Count: 3 },
{ Id: '2', Count: 1 },
{ Id: '3', Count: 1 },
{ Id: '4', Count: 0 },
{ Id: '5', Count: 0 },
{ Id: '6', Count: 0 },
{ Id: '7', Count: 1 },
{ Id: '8', Count: 0 }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment