how to know if a variable is an array or object

Options
external API returns array for multiple items or object (without [ ] ) for single item  depending on the number of items in the result (it is a bit weird but I have to integrate with this). Now when I pass to FE, it wants array otherwise does not work.  I need to find a way to push result to array if only 1 item return or pass directly if list is returned otherwise it does not work... I was trying to build a conditional logic but count does not really work as it counts list items or simple object parameters so if count >1 wont work... Also try merging to an array and array and adding an element to an array but both wont solve as an array merged with object becomes an object... Any other ideas how to solve it? I am sure  has the answer

Comments

  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
    hmmmm - sounds like you found an issue. Count works on the object because it considers it an associative array.

    Feels like there are tweaks we should make to the filters
    • is_array
    • is_object
    • safe_array


    safe_array will make it so whatever you pass to it will either use itself (if its an array) or turn it into an array of 1 of it isn't an array.

    We'll be able to get these into the next release - should be friday or monday.
  • Lukasz Kwiatkowski
    Options
    great looking forward to this!
    thanks 
  • Lukasz Kwiatkowski
    Options
     is there any update on this? 
  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
    Yes. It will be Monday. It is on our dev environment - just testing some other items.
  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
    this is now live - new filters - safe_array, is_array, is_object, is_int, is_decimal, is_text, is_bool

    safe_array should be what you want so its always an array
  • Lukasz Kwiatkowski
    Options
    hi  you guys are awesome, thanks for so much for adding it!

    I will probably be able to achieve what I want with "is_array"+ conditional however safe_array is not working with objects which was the main issue for me. is it a bug or this function always behaves that way? you can only safe array e.g. [1,2,3] or 1 but you cannot safe_array {a: 1,b: 2} - xano does not throw any errors however the statement would stay as {a: 1,b: 2} instead of [{a: 1,b: 2}]
  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
     Strange. Will double check. We are doing another build today so we can fix this if needed.
  • Lukasz Kwiatkowski
    Options
     i built a workout with "is_array" so not urgent but if you guys can fix "safe_array" I would welcome it happily as this is super neat
  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
     this is fixed