How to return an array from a Javascript function ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pankajit09
    Contributor
    • Dec 2006
    • 296

    #1

    How to return an array from a Javascript function ?

    Hello,

    How to return an array from a Javascript function ?

    For eg:

    Code:
    function test()
    {
        var test1 = document.getElementsByName("textarea");
    
        return test1;
    }
    Please correct the above code.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    If you want an array of the textareas on the page, change getElementsByNa me to getElementsByTagName.

    Comment

    • pankajit09
      Contributor
      • Dec 2006
      • 296

      #3
      Originally posted by acoder
      If you want an array of the textareas on the page, change getElementsByNa me to getElementsByTagName.

      No I want to extract textareas which have a specific name.


      Also tell me how to return associative arrays ?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Then replace "textarea" in your original code to the name of the textareas.

        Comment

        • pankajit09
          Contributor
          • Dec 2006
          • 296

          #5
          Originally posted by acoder
          Then replace "textarea" in your original code to the name of the textareas.
          That I am already doing .


          Also tell me how to return an object.

          For eg:

          Code:
          var obj = new Object(); 
          obj["firstName"]=”vijay”;
          obj["lastName"]=”khambalkar”;

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            If it's an array, use [] in place of "new Object()" and then after those statements, return obj; will return an array.

            Comment

            • pankajit09
              Contributor
              • Dec 2006
              • 296

              #7
              Originally posted by acoder
              If it's an array, use [] in place of "new Object()" and then after those statements, return obj; will return an array.

              No I want to know how to return associative arrays ?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                What does the following do?
                [code=html]<html>
                <head>
                <script>
                function test() {
                var obj = [];
                obj["firstName"]="vijay";
                obj["lastName"]="khambalkar ";
                return obj;
                }
                </script>
                </head>
                <body>
                <script>
                var t = test();
                document.write( t["firstName"]);
                </script>
                </body>
                </html>[/code]

                Comment

                • pankajit09
                  Contributor
                  • Dec 2006
                  • 296

                  #9
                  Originally posted by acoder
                  What does the following do?
                  [code=html]<html>
                  <head>
                  <script>
                  function test() {
                  var obj = [];
                  obj["firstName"]="vijay";
                  obj["lastName"]="khambalkar ";
                  return obj;
                  }
                  </script>
                  </head>
                  <body>
                  <script>
                  var t = test();
                  document.write( t["firstName"]);
                  </script>
                  </body>
                  </html>[/code]

                  I want to send the array to a PHP file using Ajax.

                  How to read the array in PHP ?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by pankajit09
                    I want to send the array to a PHP file using Ajax.

                    How to read the array in PHP ?
                    Now it makes more sense what you're trying to do.

                    Send it as a string with a unique delimiter and then split the string using that character with PHP.

                    Comment

                    • pankajit09
                      Contributor
                      • Dec 2006
                      • 296

                      #11
                      Originally posted by acoder
                      Now it makes more sense what you're trying to do.

                      Send it as a string with a unique delimiter and then split the string using that character with PHP.

                      For sending it as a string I have to traverse through the array, take each value and concatenate it.

                      Is there any other way ?

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Use the join() method to put all the elements of an array into a string.

                        Comment

                        • pankajit09
                          Contributor
                          • Dec 2006
                          • 296

                          #13
                          Originally posted by acoder
                          Use the join() method to put all the elements of an array into a string.
                          But in that case will I get the keys also ?

                          Its an associative array.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Do you need the keys too?

                            Since it's an associative array/object, use a for...in loop:
                            [code=javascript]var obj = new Object;
                            obj["whatever"] = "something" ;
                            ...
                            var str = ""
                            for (var i in obj)
                            str+=obj[i] + ":";
                            [/code]

                            Comment

                            • pankajit09
                              Contributor
                              • Dec 2006
                              • 296

                              #15
                              Originally posted by acoder
                              Do you need the keys too?

                              Since it's an associative array/object, use a for...in loop:
                              [code=javascript]var obj = new Object;
                              obj["whatever"] = "something" ;
                              ...
                              var str = ""
                              for (var i in obj)
                              str+=obj[i] + ":";
                              [/code]

                              I know this way.

                              I wanted a way to return the associative array.

                              Comment

                              Working...