PowerShell logic to remove objects from Array -


i'm trying remove objects array contain duplicates , keep ones highest number in tastecode. example below highly simplified, shows problem.

example:

$fruits name   | color  | tastecode -----    ------   --------- apple  | red    | 2 apple  | red    | 3 peer   | green  | 0 banana | yellow | 1 banana | yellow | 0 banana | yellow | 3 

desired solution:

name   | color  | tastecode -----    ------   --------- apple  | red    | 3 peer   | green  | 0 banana | yellow | 3 

i've succeeded in gathering ones need removed, doesn't work out quite well:

$duplicatemembers = $fruits | group-object name | count -ge 2 $duplicatemembers | foreach-object {      $remove = $_.group | sort-object tastecode | select -first ($_.group.count -1)      $fruits = foreach ($f in $fruits) {         foreach ($r in $remove) {             if (($f.name -ne $r.name) -and ($f.tastecode -ne $r.tastecode)) {                 $f             }         }     } } 

thank help.

how not performing remove, sort on tastecode descending , taking 1 first result?

$duplicatemembers = $fruits | group-object name  $duplicatemembers | foreach-object {     $outcome = $_.group | sort-object tastecode -descending | select -first 1     $outcome } 

this way should not bother remove form result of query.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -