Best way to check if variable is array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Kypriotis
    New Member
    • Mar 2011
    • 37

    Best way to check if variable is array?

    I have read lots and lots of possible solutions all with its drawbacks, e.g. if it from a different frame or if it is a custom object including 'array' in its constructor, etc. but all more than 3 months old. The lattest mentioned Array.isArray([]) that comes with ECMAScript 5th Edition but do not work with IE7 and lower. Wanna know if there is a sure-proof solution for all?

    P.S. For my particular application I take some arrays from JSON and want to check that they are arrays and then check teir length property to see if they have data inside
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    since JSON supports only the Object and Array objects, one of the old constructor checks is sufficient

    Comment

    • Mike Kypriotis
      New Member
      • Mar 2011
      • 37

      #3
      found this page

      which concludes using
      Code:
      function isArray(o) {
        return Object.prototype.toString.call(o) === '[object Array]';
      }
      which is my best find so far, question is though that does not say which edition of ECMA-262 he took the definition of Object.prototyp e.toString (hoping its also working for 3rd edition cause I have tested the function for only the lattest browsers -5th ed)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        toString() is a very, very basic method. that’s definitely not a recent addition

        Comment

        • Mike Kypriotis
          New Member
          • Mar 2011
          • 37

          #5
          for a non framed enviroment (just taking some data from JSON) would u recommend this or instanceof? (have read that though the above is more complete -support multi-framed is slower and more complicated )

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            framed environment?

            I’d use instanceof, the question is which version of IE supports that.

            Comment

            • Mike Kypriotis
              New Member
              • Mar 2011
              • 37

              #7
              with "framed enviroment" meant working with multiple frames/windows and have to transfer an array from on to an other. From Javascript Bible found:
              InstanceOf Compatibility: WinIE5+, MacIE-, NN6+, Moz+, Safari+, Opera+, Chrome+

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I don’t think it has an influence on testing for an array.

                Comment

                • Mike Kypriotis
                  New Member
                  • Mar 2011
                  • 37

                  #9
                  not sure what u mean, tested this code
                  Code:
                  a = new Array(1,2,3);
                  a instanceof Array; //true
                  a instanceof Object; //also true but 
                  //I am interesting in testing for arrays
                  which is what i want but read that arrays are also of type object so tested
                  Code:
                  function userobject(str1){this.name=str1;}
                  a=new userobject("nikos");
                  alert(a instanceof Array);//false
                  which means that it distinguish object from array, so I am 100% sure of this working right?

                  P.S. On the other hand if want to test for both arrays and object determine which is which I will have to go with the prev solution right?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    any non-primitive (undefined, null & the literals) inherits from Object because Object is the base of every object (including Array) in JavaScript.

                    Comment

                    • Mike Kypriotis
                      New Member
                      • Mar 2011
                      • 37

                      #11
                      So you mean that checking for undefined, null & literals with the InstanceOf Object will return true. But in case of checking with InstanceOf Array all is OK right?

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        those will return false, as they are not objects.

                        Comment

                        • Mike Kypriotis
                          New Member
                          • Mar 2011
                          • 37

                          #13
                          concluding for the job of checking some arrays in source code (not frames) and/or from JSON u would use instanceOf or Object.prototyp e.toString.call (o) === '[object Array]' ?

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            instanceof. toString() is easier to overwrite.

                            Comment

                            Working...