Creating and accessing a JavaScript multidimensional array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keiron Waites

    Creating and accessing a JavaScript multidimensional array?

    <script language="JavaS cript" type="text/javascript">
    <!--
    var array1 = new Array();
    var array1i = new Array();

    array1[0] = array1i["hello"];

    alert(array1[0][0]);
    // -->
    </script>

    Why doesn't this work?

    Thanks.


  • Douglas Crockford

    #2
    Re: Creating and accessing a JavaScript multidimensiona l array?

    > var array1 = new Array();[color=blue]
    > var array1i = new Array();
    >
    > array1[0] = array1i["hello"];
    >
    > alert(array1[0][0]);[/color]
    [color=blue]
    > Why doesn't this work?[/color]

    Perhaps you meant

    array1[0] = array1i;
    array1i[0] = "hello";

    Or

    var array1 = [["hello"]];



    Comment

    • Lee

      #3
      Re: Creating and accessing a JavaScript multidimensiona l array?

      Keiron Waites said:[color=blue]
      >
      ><script language="JavaS cript" type="text/javascript">
      ><!--
      >var array1 = new Array();
      >var array1i = new Array();
      >
      >array1[0] = array1i["hello"];
      >
      >alert(array1[0][0]);
      >// -->
      ></script>
      >
      >Why doesn't this work?[/color]

      Because it doesn't make any sense.

      I'm guessing that you meant:

      <script type="text/javascript">
      var array1 = new Array();
      var array1i = new Array("hello");
      array1[0]=array1i;
      alert(array1[0][0]);
      </script>

      Comment

      • DB McGee

        #4
        Re: Creating and accessing a JavaScript multidimensiona l array?

        Not sure if this is what you are going for, but I personally prefer to use
        an array of custom JS objects for simulating multi-dimensional arrays:

        eg. Imagine a case where you wanted to store in an array, a URL and the text
        to display for each URL. I'd start by creating a really simple JS object:

        function URL( url, name ) {
        this.url = url;
        this.name = name;
        }

        Then you can simply store each object in an 'array of objects':

        var myURLs = new Array();
        myURLs[0] = new URL ( 'www.yahoo.com' , 'Yahoo!' );
        myURLs[1] = new URL ( 'www.sitepoint. com', 'Site Point' );
        myURLs[3] = new URL ( 'www.cnn.com', 'CNN' );
        myURLs[4] = new URL ( 'www.espn.com', 'espn' );

        Then you can just access each one via the object properties. Eg. to get the
        data for the second value:

        url = myUrls[1].url
        name = myUrls[1].name

        I find that the inherent structure of a custom object approach makes it a
        lot easier to manage things - both in developing the code and whenever I
        need to revisit it later on to make any changes or extend it in any way.
        This is my personal preference anyway.

        Apologies in advance if this is confusing or off-topic!

        "Keiron Waites" <webmaster@NOSP AMsharemonkey.c om> wrote in message
        news:bn3o1b$968 $1@sparta.btint ernet.com...[color=blue]
        > <script language="JavaS cript" type="text/javascript">
        > <!--
        > var array1 = new Array();
        > var array1i = new Array();
        >
        > array1[0] = array1i["hello"];
        >
        > alert(array1[0][0]);
        > // -->
        > </script>
        >
        > Why doesn't this work?
        >
        > Thanks.
        >
        >[/color]


        Comment

        • Douglas Crockford

          #5
          Re: Creating and accessing a JavaScript multidimensiona l array?

          > Then you can simply store each object in an 'array of objects':[color=blue]
          >
          > var myURLs = new Array();
          > myURLs[0] = new URL ( 'www.yahoo.com' , 'Yahoo!' );
          > myURLs[1] = new URL ( 'www.sitepoint. com', 'Site Point' );
          > myURLs[3] = new URL ( 'www.cnn.com', 'CNN' );
          > myURLs[4] = new URL ( 'www.espn.com', 'espn' );[/color]

          Or you could use the literal array notation:

          var myURLs = [
          ['www.yahoo.com' , 'Yahoo!' ],
          ['www.sitepoint. com', 'Site Point'],
          ['www.cnn.com', 'CNN'],
          ['www.espn.com', 'espn']];



          Comment

          • Kent Feiler

            #6
            Re: Creating and accessing a JavaScript multidimensiona l array?

            > <script language="JavaS cript" type="text/javascript">[color=blue]
            > <!--
            > var array1 = new Array();
            > var array1i = new Array();
            >
            > array1[0] = array1i["hello"];
            >
            > alert(array1[0][0]);
            > // -->
            > </script>
            >
            > Why doesn't this work?
            >
            > Thanks.[/color]
            ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~
            Array1 is only one-diminsional so the second [0] is meaningless. You
            may want to do something like this:

            var array1 = new Array( new Array(), new Array() );

            Now alert( array1[0][0] ); will work.




            Regards,


            Kent Feiler

            Comment

            Working...