Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Last active October 12, 2021 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/2fdb0c7097545b4c86ddf60fe7fb5056 to your computer and use it in GitHub Desktop.
Save IISResetMe/2fdb0c7097545b4c86ddf60fe7fb5056 to your computer and use it in GitHub Desktop.
using namespace System.Collections
function flatten
{
param(
[IDictionary]$Dictionary,
[string]$KeyDelimiter = ':'
)
$newDict = @{}
$stackOfTrees = [Stack]::new()
foreach($kvp in $Dictionary.GetEnumerator()){
$stackOfTrees.Push(@($kvp.Key,$kvp.Value))
}
while($stackOfTrees.Count -gt 0)
{
$prefix,$next = $stackOfTrees.Pop()
if($next -is [IDictionary]){
foreach($kvp in $next.GetEnumerator())
{
$stackOfTrees.Push(@("${prefix}${KeyDelimiter}$($kvp.Key)", $kvp.Value))
}
}
else {
$newDict["${prefix}"] = $next
}
}
return $newDict
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment