iRon7 / Join-Object

Combines two objects lists based on a related property between them.
MIT License
107 stars 13 forks source link

Support non-object arrays #14

Closed iRon7 closed 3 years ago

iRon7 commented 3 years ago

Especially in a case of a side-by-side joins (where the -on parameter is omitted), it would be handy to be able join an array of something that is not an object (meaning anything that doesn't have visual properties like strings and primitives) to an complex (PowerShell) object list, or to another array of non-objects.

iRon7 commented 3 years ago

Implementation

(Version 3.4.0 and up)

If you join on or more scalar arrays, a scalar array will be return which can be easily used in a foreach loop:

$a = 'a1', 'a2', 'a3', 'a4'
$b = 'b1', 'b2', 'b3', 'b4'
$c = 'c1', 'c2', 'c3', 'c4'
$d = 'd1', 'd2', 'd3', 'd4'

Example 1: Default join of multiple scalar arrays

$a |Join $b |Join $c |Join $d |% { "$_" } 
# or: foreach ($row in ($a |Join $b |Join $c |Join $d)) { "$Row" }
# or: $a |Join $b |Join $c |Join $d |% { '{0} {1} {2} {3}' -f $_[0], $_[1], $_[2], $_[3] }  

a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

Example 2: Naming items

$a |Join $b |Join $c |Join $d -Name a, b, c, d

a  b  c  d
-  -  -  -
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

Example 3: Joining a scalar array with an object array:

$Id = 'a1', 'a2', 'a3', 'a4'
$Department = ConvertFrom-Csv @'
"Name",        "Country"
"Engineering", "Germany",
"Marketing",   "England",
"Sales",       "France",
"Purchase",    "France"
'@

$Id |Join $Department

Value Name        Country
----- ----        -------
a1    Engineering Germany
a2    Marketing   England
a3    Sales       France
a4    Purchase    France

Note that the scalar column will be named Value by default when it joined with a PowerShell object, to change this use the -ValueName parameter, e.g.: $Id |Join $Department -ValueName Id

iRon7 commented 3 years ago

Implemented in version 3.4.0