Distinct an array of objects using JavaScript

I have an array of objects where I wanted make a list of array which is unique while at the same time adding different values to sub property into the distinct object. Example below is an array of a mixture of folders and files, together with it are it details include used size, access users and etc.
In AccessUsers array, there are Accounts their rights :

Array [
  0: Object {
    FullPath: c:\imod-vs-solutions\centrallogging\testzip
    TotalContents: 3
    UsedSize: 26112
    IsDir: true
    CheckSum: [NULL]
    AccessUsers: Array [
      0: Object {
        Account: NT AUTHORITY\SYSTEM
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      1: Object {
        Account: BUILTIN\ADMINISTRATORS
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      2: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: READ_ONLY
        ]
      }
      3: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: xxxxxxxx
        ]
      }
      4: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ContainerInherit, Inherited
        Rights: Array [
          0: APPEND
        ]
      }
      5: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ContainerInherit, Inherited
        Rights: Array [
          0: WRITE
        ]
      }
      6: Object {
        Account: SM\SW
        AceType: AccessAllowed
        AceFlags: Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      7: Object {
        Account: \CREATOR OWNER
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, InheritOnly, Inherited
        Rights: Array [
          0: FULL_CTRL_SUB_ONLY
        ]
      }
    ]
    Size: 25.5 KB
  }
  1: Object {
    FullPath: c:\imod-vs-solutions\centrallogging\testzip\abc
    TotalContents: 2
    UsedSize: 26
    IsDir: true
    CheckSum: [NULL]
    AccessUsers: Array [
      0: Object {
        Account: NT AUTHORITY\SYSTEM
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      1: Object {
        Account: BUILTIN\ADMINISTRATORS
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      2: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: READ_ONLY
        ]
      }
      3: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ContainerInherit, Inherited
        Rights: Array [
          0: APPEND
        ]
      }
      4: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ContainerInherit, Inherited
        Rights: Array [
          0: WRITE
        ]
      }
      5: Object {
        Account: SM\SW
        AceType: AccessAllowed
        AceFlags: Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      6: Object {
        Account: \CREATOR OWNER
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, InheritOnly, Inherited
        Rights: Array [
          0: FULL_CTRL_SUB_ONLY
        ]
      }
    ]
    Size: 26 B 
  }
  2: Object {
    FullPath: C:\sss\centrallogging\testzip\abc\aaaaaaaaaaaa.txt
    TotalContents: 0
    UsedSize: 13
    IsDir: false
    CheckSum: C070D3A9E487FAvvvvvvvvvvvvvvvvvvvvvvvvvv9A1DB1BA4A0449999500A68C71DFA82866
    AccessUsers: [NULL]
    Size: 13 B 
  }
  3: Object {
    FullPath: C:\sss\centrallogging\testzip\abc\xxxxx.txt
    TotalContents: 0
    UsedSize: 13
    IsDir: false
    CheckSum:C070D3A9E487FAwwwwwwwwwwwwwwwwwwwww1DB1BA4A0449999500A68C71DFA82866
    AccessUsers: [NULL]
    Size: 13 B 
  }
  4: Object {
    FullPath: C:\sss\centrallogging\testzip\CentralLoggingClient.exe
    TotalContents: 0
    UsedSize: 26112
    IsDir: false
    CheckSum: 537C3811dddddddddddddddddddddddddddd836FFF72DCD611F6E004D5D69FB
    AccessUsers: [NULL]
    Size: 25.5 KB
  }
  5: Object {
    FullPath: c:\sss\centrallogging\testzip\emptydir
    TotalContents: 0
    UsedSize: 0
    IsDir: true
    CheckSum: [NULL]
    AccessUsers: Array [
      0: Object {
        Account: NT AUTHORITY\SYSTEM
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      1: Object {
        Account: BUILTIN\ADMINISTRATORS
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      2: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, Inherited
        Rights: Array [
          0: READ_ONLY
        ]
      }
      3: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ContainerInherit, Inherited
        Rights: Array [
          0: APPEND
        ]
      }
      4: Object {
        Account: BUILTIN\USERS
        AceType: AccessAllowed
        AceFlags: ContainerInherit, Inherited
        Rights: Array [
          0: WRITE
        ]
      }
      5: Object {
        Account: SM-DEMO\sssssss
        AceType: AccessAllowed
        AceFlags: Inherited
        Rights: Array [
          0: FULL_CONTROL
        ]
      }
      6: Object {
        Account: \CREATOR OWNER
        AceType: AccessAllowed
        AceFlags: ObjectInherit, ContainerInherit, InheritOnly, Inherited
        Rights: Array [
          0: FULL_CTRL_SUB_ONLY
        ]
      }
    ]
    Size: 0 B 
  }
]

Result:

Array [
  0: Object {
    FullPath: c:\sss\centrallogging\testzip
    AccessUsers: Array [
      0: Object {
        Account: NT AUTHORITY\SYSTEM
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      1: Object {
        Account: BUILTIN\ADMINISTRATORS
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      2: Object {
        Account: BUILTIN\USERS
        AccessAllowed: Array [
          0: READ_ONLY
          1: xxxxxxxx
          2: APPEND
          3: WRITE
        ]
        AccessDenied: Array []
      }
      3: Object {
        Account: SM\SW
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      4: Object {
        Account: \CREATOR OWNER
        AccessAllowed: Array [
          0: FULL_CTRL_SUB_ONLY
        ]
        AccessDenied: Array []
      }
    ]
  }
  1: Object {
    FullPath: c:\sss\centrallogging\testzip\abc
    AccessUsers: Array [
      0: Object {
        Account: NT AUTHORITY\SYSTEM
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      1: Object {
        Account: BUILTIN\ADMINISTRATORS
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      2: Object {
        Account: BUILTIN\USERS
        AccessAllowed: Array [
          0: READ_ONLY
          1: APPEND
          2: WRITE
        ]
        AccessDenied: Array []
      }
      3: Object {
        Account: SM\SW
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      4: Object {
        Account: \CREATOR OWNER
        AccessAllowed: Array [
          0: FULL_CTRL_SUB_ONLY
        ]
        AccessDenied: Array []
      }
    ]
  }
  2: Object {
    FullPath: c:\sss\centrallogging\testzip\emptydir
    AccessUsers: Array [
      0: Object {
        Account: NT AUTHORITY\SYSTEM
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      1: Object {
        Account: BUILTIN\ADMINISTRATORS
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      2: Object {
        Account: BUILTIN\USERS
        AccessAllowed: Array [
          0: READ_ONLY
          1: APPEND
          2: WRITE
        ]
        AccessDenied: Array []
      }
      3: Object {
        Account: SM\SW
        AccessAllowed: Array [
          0: FULL_CONTROL
        ]
        AccessDenied: Array []
      }
      4: Object {
        Account: \CREATOR OWNER
        AccessAllowed: Array [
          0: FULL_CTRL_SUB_ONLY
        ]
        AccessDenied: Array []
      }
    ]
  }
]

Base on example above I have to distinct different directory full path and add in the rights in the array of access type. Below are the code to do the trick.

function sdManupulateFolderDetails(scanData, isDir) {
	var r = [];
	for(var j = 0; j < scanData.length; j++) {
		if (scanData[j].IsDir === isDir) {
			var dir = scanData[j];
			var du = {}; var userAccessRights = [];
			for(var i = 0; i < dir.AccessUsers.length; i++) {
				var acl = dir.AccessUsers[i];
				var usrName = acl.Account;
				if (!du[usrName]) {
					du[usrName] = {Account: usrName, AccessAllowed: [], AccessDenied: []};
					userAccessRights.push(du[usrName]);
				}
				for(var k=0; k <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>&lt; acl.Rights.length; k++) {
					du[usrName][acl.AceType].push(acl.Rights[k]);
				}
				userAccessRights = userAccessRights.sort();

			}
			r.push({FullPath: dir.FullPath, AccessUsers: userAccessRights});
		}
	}
	return r;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.