Flatten array in PowerShell

后端 未结 3 963
天涯浪人
天涯浪人 2021-02-07 01:29

Assume we have:

$a = @(1, @(2, @(3)))

I would like to flatten $a to get @(1, 2, 3).

I have found one solution

3条回答
  •  天涯浪人
    2021-02-07 02:11

    Piping is the correct way to flatten nested structures, so I'm not sure what would be more "elegant". Yes, the syntax is a bit line-noisy looking, but frankly quite serviceable.

    2020 Edit

    The recommended syntax these days is to expand % to ForEach-Object. A bit more verbose but definitely more readable:

    @($a | ForEach-Object {$_}).count
    

提交回复
热议问题