我必须创建一个JavaScript函数,将一个波纹管这样的对象转换为分层JSON对象。
来源是人力资源员工列表及其相应的经理。
源对象:
Employee Manager Title
Alfredo general manager
Bergman Alfredo senior engineer
Billy Alfredo senior engineer
Johnson Alfredo department manager
Angela Johnson senior engineer
Anderson Johnson senior engineer
Amy Johnson department manager
Patsy Amy engineer
Jane Amy engineer
这是JSON对象的外观:
var datasource = {
'name': 'Alfredo',
'title': 'general manager',
'children': [
{ 'name': 'Bergman', 'title': 'senior engineer' },
{ 'name': 'Johnson', 'title': 'department manager',
'children': [
{ 'name': 'Anderson', 'title': 'senior engineer' },
{ 'name': 'Amy', 'title': 'department manager',
'children': [
{ 'name': 'Patsy', 'title': 'engineer'},
{ 'name': 'Jane', 'title': 'engineer'}
]
},
{ 'name': 'Angela', 'title': 'senior engineer' }
]
},
{ 'name': 'Billy', 'title': 'senior engineer' }
]
};
我尝试了各种方法均未成功,但是我对JavaScript相当陌生。
任何帮助或想法都会受到赞赏。
答案 0 :(得分:2)
我认为这应该做您想要做的事情:遍历列表,构建所有员工的地图,然后按照经理的姓名将员工按正确的子级排序,仅返回符合条件的员工没有经理。
$date = Get-Date -DisplayHint Date -UFormat "%d/%m/%Y"
$time = Get-Date -DisplayHint Time
$ThresholdFileCount = 1
$smtp = "address"
$to = "ok@mail.com"
$from = "abc@def.com"
$subject = "Number of files is above threshold - $date"
$style = "<style>
TABLE {border-width: 1px; border-style: solid; border-color:black; border-
collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid;border-color:
black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color:
black;}
</style>"
function filecount {
param ([string]$path)
$body += "<body style='font-family: Cambria; font-size: 16pt;'><b><u><a
name='Monitor'>Monitor</a></u></b></body><br>"
if (-not (Test-Path $path)) {Throw "Path: $path not found"}
$count = 0
$count = Get-ChildItem -Path $path | where {!$_.PSIsContainer} | Measure-
Object | select -ExpandProperty count
if ($count -gt $ThresholdFileCount) {
$details = Get-Item -Path $path| Select-Object PSDrive, @{N = "Parent"; E =
{($_.PSParentPath -split "FileSystem::")[1]}
}, Name, @{N = "FileCount"; E =
{$count}
}
$body += $details |ConvertTo-Html
Write-Output $details
}
}
Send-MailMessage -FROM $from -To $to -Subject $subject -Body $body BodyAsHtml -Priority High -SmtpServer $smtp
filecount "address"
答案 1 :(得分:1)
您可以通过使用员工与经理之间的关系来采取单循环方法,反之亦然。
var data = [{ name: 'Bergman', manager: 'Alfredo', title: 'senior engineer' }, { name: 'Billy', manager: 'Alfredo', title: 'senior engineer' }, { name: 'Johnson', manager: 'Alfredo', title: 'department manager' }, { name: 'Angela', manager: 'Johnson', title: 'senior engineer' }, { name: 'Anderson', manager: 'Johnson', title: 'senior engineer' }, { name: 'Amy', manager: 'Johnson', title: 'department manager' }, { name: 'Patsy', manager: 'Amy', title: 'engineer' }, { name: 'Jane', manager: 'Amy', title: 'engineer' }, { name: 'Alfredo', manager: '', title: 'general manager' }],
tree = function (data, root) {
var o = {};
data.forEach(function ({ name, manager, title }) {
Object.assign(o[name] = o[name] || {}, { name, title }, o[name].children && { children: o[name].children });
o[manager] = o[manager] || { name: null, title: null };
o[manager].children = o[manager].children || [];
o[manager].children.push(o[name]);
});
return o[root].children;
}(data, '');
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }