我有一个数组,我想重新显示为一个结构层次:
Id Name ParentRoleId
00E6T000002G5sXUAS APAC AE, Commercial 00E6T000002G3skUAC
00E6T000002G5scUAC APAC AE, Enterprise 00E6T000002G3skUAC
00E0W000001urB6UAI Account Executive, Industry Solutions 00E0W000001ur6pUAA
00E6T000002G2w7UAC Commercial - Account Management Team 1 00E6T000002G2w2UAC
00E6T000002G61ZUAS Commercial AE Roll-up 00E0W000002FqrtUAC
00E0W000001uhZBUAY Commercial AE, Team 1 00E0W000001dhnOUAQ
00E6T000002G5s8UAC Commercial AE, Team 2 00E0W000001uhZ6UAI
00E6T000002G5sIUAS Commercial AE, Team 3 00E6T000002G5sDUAS
00E0W00000273AgUAI Commercial East CSM Manager 00E0W000002G1qwUAC
00E6T000002G5zOUAS Commercial East CSM Manager, Team 1 00E0W00000273AgUAI
00E0W00000273AlUAI Commercial East CSM Rep 00E0W00000273AgUAI
00E6T000002G5zTUAS Commercial East CSM, Team 1 00E6T000002G5zOUAS
00E0W000001dm1wUAA Commercial - Account Management Team 2 00E6T000002G5tLUAS
00E0W0000023PkrUAE Director, Mid-Market 00E0W000001yQAYUA2
00E0W000001dmP0UAI Commercial Sales Engineer 00E0W000001dmOvUAI
00E0W000001dmOvUAI Commercial Sales Engineer Manager 00E0W000001dio6UAA
00E0W000001dlGvUAI Commercial West CSM Manager 00E0W000002G1qwUAC
00E6T000002G5xmUAC Commercial West CSM Manager, Team 1 00E0W000001dlGvUAI
00E0W000001dlH0UAI Commercial West CSM Rep 00E0W000001dlGvUAI
00E6T000002G5xwUAC Commercial West CSM Rep, Team 1 00E6T000002G5xmUAC
00E6T000002G5FuUAK CSM, EMEA 00E6T000002G5FpUAK
00E6T000002G5FpUAK CSM Manager, EMEA 00E0W000002G1qwUAC
00Ed0000000ajHUEAY Customer Success 00Ed0000000ZbFrEAK
00E6T000002G3I8UAK Customer Growth Sales Engineer 00E6T000002G3I3UAK
00E6T000002G3I3UAK Customer Growth Sales Engineer Manager 00E0W000001dio6UAA
00E0W000001dkxtUAA Director, Commercial Account Management 00E0W000001uqtCUAQ
00E0W000001dhnOUAQ Director, Commercial Team 1 - Rachel White 00E6T000002G61ZUAS
00E0W000001uhZ6UAI Director, Commercial Team 2 - Joe DeFrance 00E6T000002G61ZUAS
00E6T000002G5sDUAS Director, Commercial Team 3 - Maya Connet 00E6T000002G61ZUAS
00E6T000002G5smUAC Director, EMEA Commercial 00E6T000001uuGJUAY
00E6T000002G5swUAC Director, EMEA Enterprise 00E6T000001uuGJUAY
00E6T000002G2mRUAS Director, Emerging Team 1 - Adam Wainwright 00E6T000002G61eUAC
00E6T000002G5sNUAS Director, Emerging Team 2 - Kristen Meier 00E6T000002G61eUAC
00E0W000001ur9jUAA Director, Enterprise Account Management 00E0W000001uqtCUAQ
00E0W000001yQAdUAM Director, Enterprise Team 1 - Tatum Savage 00E0W000001yQAYUA2
00E6T000002G5tBUAS Director, Enterprise Team 2 - Jason Plank 00E0W000001yQAYUA2
00E0W000001dkqnUAA Director, Revenue Development 00E0W000001dioBUAQ
00E6T000002G2t8UAC EMEA - Account Management 00E6T000002G61KUAS
00E6T000002G5srUAC EMEA AE, Commercial 00E6T000002G5smUAC
00E6T000002G5t1UAC EMEA AE, Enterprise 00E6T000002G5swUAC
00E6T000002G3IIUA0 EMEA Sales Engineer 00E6T000002G3IDUA0
00E6T000002G3IDUA0 EMEA Sales Engineer Manager 00E0W000001dio6UAA
00E6T000002G2DqUAK EMEA Support 00E0W00000272uxUAA
00E6T000002G61eUAC Emerging AE Roll-up 00E0W000002FqrtUAC
00E6T000002G2ntUAC Emerging AE, Team 1 00E6T000002G2mRUAS
00E6T000002G5sSUAS Emerging AE, Team 2 00E6T000002G5sNUAS
00E6T000002G2ymUAC Enterprise - Account Management Team 1 00E6T000002G2yhUAC
00E6T000002G5t6UAC Enterprise AE, Team 1 00E0W000001yQAdUAM
00E6T000002G5tGUAS Enterprise AE, Team 2 00E6T000002G5tBUAS
00E0W000001uqtMUAQ Enterprise - Account Management 00E0W000001ur9jUAA
00E0W00000273AWUAY Enterprise East CSM Manager 00E0W000002G1qwUAC
00E0W00000273AbUAI Enterprise East CSM Rep 00E0W00000273AWUAY
00Ed0000002JGNWEA4 Enterprise Sales Engineer 00E0W000001dmOqUAI
00E0W000001dmOqUAI Enterprise Sales Engineer Manager 00E0W000001dio6UAA
00E0W000002G1qmUAC Enterprise West CSM Manager 00E0W000002G1qwUAC
00E0W000002G1qrUAC Enterprise West CSM Rep 00E0W000002G1qmUAC
00Ed0000000ZbFrEAK Executive 00E0W000002B61KUAS
00E0W000001dioGUAQ Finance 00Ed0000000ZbFrEAK
00E0W000001dio6UAA Head of Sales Engineering 00Ed0000000ZbFrEAK
00E0W000001hzpnUAA Implementation Reps 00Ed0000000ajHUEAY
00E6T000002G2DvUAK India Support 00E6T000002G2E5UAK
00E6T000002G2E5UAK India Support Manager 00E0W00000272uxUAA
00E0W000002Npc7UAC InfoSec 00E0W000002B61KUAS
00E0W000001uVmIUAU Integration
00E6T000002G2w2UAC Manager, Commercial Account Management 00E0W000001dkxtUAA
00E6T000002G5tLUAS Manager, Commercial Account Management Team 2 00E0W000001dkxtUAA
00E6T000002G61KUAS Manager, EMEA Account Management 00E0W000001dkxtUAA
00E6T000002G2yhUAC Manager, Enterprise Account Management 00E0W000001ur9jUAA
00Ed0000002JGNMEA4 Marketing 00Ed0000000ZbFrEAK
00E6T000002G5tVUAS Mid-Market - Account Manager 00E6T000002G61jUAC
00E0W000001uhtaUAA Mid-Market AE 00E0W0000023PkrUAE
00E6T000002G61jUAC Mid-Market AM Roll-up 00E0W000001dkxtUAA
00Ed0000002JGNREA4 Product Management 00Ed0000000ZbFrEAK
00E6T000002G5taUAC RevDev Manager, ADM 00E0W000001dkqnUAA
00E6T000002G5G4UAK RevDev Manager, EMEA 00E0W000001dkqnUAA
00E0W000001dkr2UAA RevDev Manager, Emg / Comm 00E0W000001dkqnUAA
00E6T000002G5tzUAC RevDev Manager, Industries / AMER Wingman 00E0W000001dkqnUAA
00E0W000001dkqsUAA RevDev Manager, MM / Ent / India Wingman 00E0W000001dkqnUAA
00E6T000002G5tfUAC RevDev Rep, ADM 00E6T000002G5taUAC
00E6T000001uuO8UAI RevDev Rep, AMER Wingman 00E6T000002G5tzUAC
00E6T000002G7jIUAS RevDev Rep, APAC 00E0W000001dkqsUAA
00E6T000002G5tuUAC RevDev Rep, Commercial 00E0W000001dkr2UAA
00E6T000002G5tkUAC RevDev Rep, EMEA Commercial 00E6T000002G5smUAC
00E6T000002G5tpUAC RevDev Rep, EMEA Enterprise 00E6T000002G5G4UAK
00E0W000001dkr7UAA RevDev Rep, Emerging 00E0W000001dkr2UAA
00E0W000001dkqxUAA RevDev Rep, Enterprise 00E0W000001dkqsUAA
00E6T000002G7YyUAK RevDev Rep, Executive Program 00E0W000001dkqnUAA
00E6T000001uuODUAY RevDev Rep, India Wingman 00E0W000001dkqsUAA
00E6T000002G5u4UAC RevDev Rep, Industries 00E6T000002G5tzUAC
00E6T000002G5u9UAC RevDev Rep, Mid-Market 00E0W000001dkqsUAA
00Ed0000000ZbFwEAK Sales Management 00Ed0000000ZbFrEAK
00E0W000002B61KUAS Sales Operations/Admin
00E6T000002G3INUA0 Solutions Engineering 00E0W000001dio6UAA
00E6T000002G74PUAS Sr. Manager of Growth Strategy 00E0W000001dio6UAA
00E6T000002G2Y5UAK Strategic CSM 00E0W000002G1r1UAC
00E0W000002G1r1UAC Strategic CSM Team Lead 00E0W000002G1qwUAC
00E0W00000272uxUAA Support Team 00Ed0000000ajHUEAY
00E6T000002G3TGUA0 SVP of Global Sales 00Ed0000000ZbFwEAK
00E6T000002G2E0UAK US Support 00E0W00000272uxUAA
00E0W000001uqtCUAQ VP, Account Management 00Ed0000000ZbFwEAK
00E0W000002FqrtUAC VP Commercial and Emerging 00E6T000002G3TGUA0
00E0W000002G1qwUAC VP Customer Success Management 00Ed0000000ajHUEAY
00E0W000001dioBUAQ VP, Growth & Enablement 00Ed0000000ZbFrEAK
00E6T000001uuGJUAY VP, International Sales 00E6T000002G3TGUA0
00E0W000001yQAYUA2 VP Mid-Market and Enterprise 00E6T000002G3TGUA0
00E6T000002G3skUAC VP of Sales, APAC 00E6T000001uuGJUAY
00E0W000001ur6pUAA VP, Verticals & Solutions 00E6T000002G3TGUA0
00E6T000001uuO3UAI Wingman POD AE 00E6T000001uuNyUAI
00E6T000001uuNyUAI Wingman POD Sales Management 00E0W000002FqrtUAC
00E6T000001uuSKUAY Wingman-Side, Customer Success 00E0W000001uqtCUAQ
00E6T000001uu9NUAQ Wingman-side Sales 00E6T000001uuNyUAI
层次结构的顶部是那些没有parentroleID的对象,我想循环遍历并获取子对象,然后显示如下:
姓名(id):
Integration (00E0W000001uVmIUAU)
Sales Operations/Admin (00E0W000002B61KUAS)
-Executive (00Ed0000000ZbFrEAK)
-Finance (00E0W000001dioGUAQ)
-Head of Sales Engineering (00E0W000001dio6UAA)
-Enterprise Sales Engineer Manager (00E0W000001dmOqUAI)
-Solutions Engineering (00E6T000002G3INUA0)
-Sr. Manager of Growth Strategy... #...and so on
我创建了一个函数:
function GetChildren ($param)
{
Foreach ($x in $param) {
($hierarchyarray | where {$_.parentroleid -EQ $x}).id
}
}
并设置一个变量:$root = $层次数组|其中{$_.父代ID-EQ $null}
我可以通过使用$root.id调用函数来获取根的子对象,但我不确定如何递归地到达结构的底部
Thank you in advance
1条答案
按热度按时间y53ybaqx1#
一种方法是:
为每个项目添加“Children”属性:
然后找到根节点并递归地遍历它们的子树:
你需要这个函数来完成递归部分:
关键是每次向下移动一个子级别时,
$Depth
都将增加($Depth+1)
,这样输出就缩进了一个额外的级别。使用示例数据,输出为: