不要将任何重复的值输出到csv文件

我正在将json文件转换为csv文件。我在csv文件中输出了四列,但我不想输出任何重复的值。

我正在将变量添加到数组并将其输出到csv文件,并且如果“名称”列中有任何重复的值,因此如果发生重复,则不要将其添加到输出中。我不太确定该怎么做。

 ForEach ($fileEnvironment in $jsonVariables.ScopeValues.Environments) { 
        #Create empty array
        $dataArray = @()

      ForEach ($fileVariable in $jsonVariables.Variables) {
         if($fileVariable.Description -eq $null) {
            $fileVariable.Description = 'No Description'
        }
            if (!!$fileVariable.Scope) {
                if (!!$fileVariable.Scope.Environment) {
                    if ($fileEnvironment.Id -eq $fileVariable.Scope.Environment[0]) {                   
                        #Add fileVariable to the array
                        $dataArray += $fileVariable

                    } 
                }
            }
        }

  $filename="$($outputPath)\$($projectName)_$($fileEnvironment.Name).csv"
            if ($dataArray.Count -ne 0) {
                 $dataArray | Select-Object -Property 
  Name,Type,Value,Description | Export-Csv -notypeInformation -Path 
  $fileName
        }            

我不希望Name列具有重复的值,以便可以看到apiconfig被重复,并且我只希望将一个apiconfig输出到csv。在csv文件中的输出是这样的:     名称类型值说明     apiConfig字符串...否描述     apiconfig字符串...没有说明

leo0724 回答:不要将任何重复的值输出到csv文件

您可以使用HashSet来过滤出重复项。

这是一个说明想法的控制台日志。

$listWithDuplicates = @([PSCustomObject]@{Name="A";Desc="1"},[PSCustomObject]@{Name="A";Desc="2"},[PSCustomObject]@{Name="B";Desc="3"})

▷ $listWithDuplicates

Name Desc
---- ----
A    1
A    2
B    3

▷ $set = New-Object "System.Collections.Generic.HashSet[string]"
▷ $listWithoutDuplicates = $listWithDuplicates  |? {$set.Add($_.Name)}
▷ $listWithoutDuplicates

Name Desc
---- ----
A    1
B    3
本文链接:https://www.f2er.com/3165255.html

大家都在问