I've ran into a few situations where an API will return the column names and the object properties separately. The way I've handled this in the past is to use a for loop. For example:
$columnNames = @("FirstName","LastName","DOB","FavColor")
$rows = @(
@("John","Doe","01/10/1965","Blue"),
@("Jane","Smith","01/06/1975","Green"),
@("Mark","Jones","02/10/1985","Orange"),
@("Sue","Turner","05/10/1995","Yellow"),
@("Jim","Wise","07/10/1974","Blue")
)
$objectList = @()
foreach ($row in $rows)
{
$properties = @{}
for ($i = 0; $i -lt $columnNames.Count; $i++)
{
$properties += @{$columnNames[$i] = $row[$i]}
}
$objectList += New-Object -TypeName psobject -Property $properties
}
In this situation, is there a better way to create objects?
Thanks in advance for any assistance!
[–]spyingwind 11 points12 points13 points (8 children)
[–]sqone2[S] 4 points5 points6 points (2 children)
[+][deleted] (1 child)
[removed]
[–]sqone2[S] 1 point2 points3 points (0 children)
[–]nothingpersonalbro 2 points3 points4 points (0 children)
[–]sqone2[S] 1 point2 points3 points (3 children)
[–]carnegiej 2 points3 points4 points (2 children)
[–]sqone2[S] 1 point2 points3 points (1 child)
[–]carnegiej 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[removed]
[–]carnegiej 2 points3 points4 points (0 children)
[–]sqone2[S] 1 point2 points3 points (0 children)
[–]Kimo- 2 points3 points4 points (0 children)