Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a variable/property is a single value or is an object/array

Tags:

powershell

I want to loop through a bunch of objects and check each object's properties to see if it is a single value (string or int) or if it is an object/array (doesn't matter if it contains anything just see if it is an object or array).

I was going to just do a check to see if each property is a string or and integer, but is there a more efficient way?

like image 421
red888 Avatar asked Oct 14 '25 09:10

red888


1 Answers

$object -is [Array]

The -is operator checks whether an object is a certain type.


Edit: maybe what you want is to see if it's a Value Type?

$object.GetType().IsValueType

Update: TamusJRoyce mentioned in a comment that they didn't see an .IsValueType property, and suggested this, which is more idiomatic and might work in more situations:

$value -is [System.ValueType]
like image 91
briantist Avatar answered Oct 18 '25 04:10

briantist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!