开发者

Parse an object and add a member property to each member

I have an object which looks for example like this

Name      Number
----      ------
John      one
Major     two
Mars      one

I want to go through each member and check on Number and add a property that in the end it looks like this.

Name      Number   开发者_C百科  IsItOne
----      ------     -------
John      one        True
Major     two        False
Mars      one        True

What I got so far is do a foreach loop through the object but then I have two objects and have no chance as far as I know to change the original object.


Just another (shorter) version:

$obj | add-member -type scriptproperty -name IsItOne -value {$this.Number -eq 'one'} -passthru


It seems to be like you are talking about a set of objects with properties Name and Number.

If so, you can do like this:

$a | %{  $isitone = $false; if($_.Number -eq "one") {$isitone=$true} $_ | 
         add-member -Type noteproperty -name IsItOne -value $isitone  }


Here is a possible alternative.

function new-stuff ($name, $number) {
    New-Object PSObject -Property @{name=$name; number=$number}
}

$(
    new-stuff John  one
    new-stuff Major two
    new-stuff Mars  one
) | ForEach { $_ | Add-Member -PassThru ScriptProperty IsItOne {$this.Number-eq"one"} }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜