Forum Discussion

jose_pita's avatar
jose_pita
Super Contributor
12 years ago

array instanceOf array

Hi There,



I have to verify the content of a variable without knowing if it is a string or an array so I'm doing:



if((variable instanceof Array) == false && variable =="xxxx")

  //work with the string here



but the instanceOf is always false even if I get an array...



Anyone knows what's happening or an alternative to this?
  • I tried this and it seems to return consistent types as strings: 



    Object.prototype.toString.call(variable)



  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Jose,



    Have you checked out aqObject.GetVarType(VarParamhere ?



    Sincerely
  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Guys,



    Here is the support anwer to the GetVarType, Object.Prototype uncertainty:



    Hello Simon,

     

    Date and Array are the intrinsic JScript objects. They are not OLE-compatible data types, so it is correct that the GetVarType method treats them as objects that implement the IDispatch interface.

     

    The "Object.prototype" approach you are currently using to detect the type of a JScript object is correct.

     

    Best regards,

    Natalie Borovykh




    Sincerely

  • jose_pita's avatar
    jose_pita
    Super Contributor
    Thanks Simon, but I found something better.



    That function, in case of an array:




    var inteiro = ["aaa","bbb"]; 


     Log.Message(aqObject.GetVarType(inteiro));



    Gives me the code 9, wich is not an array. It works for strings though...



    I found this function that works:




    function _typeof(value) 




      switch(typeof(value)) 


      {  


        case "string": 


          return (value.match(/\d{1,2}[\/.-]\d{1,2}[\/.-]\d{2,4}/) != null) ? "date" : "string"; 


          break; 


        case "object": 


          try 


          { value.join() } 


          catch(e) 


          { 


            if(e.number == -2146827850) 


              return "object" 


            else 


              throw e; 


          } 


          return "array"; 


          break; 


        default: 


          return typeof(value); 


      }  


    }



     




  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi All,



    Scott's solution works no problem there but in my opinion aqObject.GetVarType should return the right  array type so I opened a support ticket: M0116592.



    I will keep you guys posted.



    BTW, ((new Array("1","2") instanceof Array) returns true ;-)



    Sincerely