For example, I want to extract the values from a key, but that key sometimes contains an object (I mean just one value) or sometimes contains an array (i mean multiples values). HOw check if there is an array or there is an object? thanks.
isArray() method is used to check if an object is an array. The Array. isArray() method returns true if an object is an array, otherwise returns false .
To check if a value is an array of specific type in TypeScript: Use the Array. isArray() method to check if the value is an array.
isArray()" to check whether the element is Java script array or not. jQuery. isArray() returns a boolean value that indicates whether the object is a JavaScript array or not. It takes the variable name as an argument and returns the result back to caller.
The isArray() method returns true if an object is an array, otherwise false .
Use the type
function:
type
The type function returns the type of its argument as a string, which is one of null, boolean, number, string, array or object.
Example 1:
echo '[0, false, [], {}, null, "hello"]' | jq 'map(type)'
[
"number",
"boolean",
"array",
"object",
"null",
"string"
]
Example 2:
echo '[0,1]' | jq 'if type=="array" then "yes" else "no" end'
"yes"
Example 3:
echo '{"0":0,"1":1}' | jq 'if type=="array" then "yes" else "no" end'
"no"
I have fields that are sometimes strings sometimes arrays and I want to iterate over them. This handles this case:
... | if type=="string" then [.] else . end | .[] | ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With