Array indexing problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Antti Nummiaho

    Array indexing problem

    Consider the following javascript:

    var temp = new Array(new Array(0))
    document.writel n(temp[0][0])

    temp = new Array(new Array(0,1))
    document.writel n(temp[0][0])

    One would assume that it would print "0 0" that is the first elements
    of the arrays, but it prints "undefined 0". Why does temp[0][0] return
    undefined when there is only one element in the array but returns the
    first element correctly when there are at least two elements?

    --
    Antti
  • to heave chunks

    #2
    Re: Array indexing problem

    >Consider the following javascript:[color=blue]
    >
    >var temp = new Array(new Array(0))
    >document.write ln(temp[0][0])
    >
    >temp = new Array(new Array(0,1))
    >document.write ln(temp[0][0])
    >
    >One would assume that it would print "0 0" that is the first elements
    >of the arrays, but it prints "undefined 0". Why does temp[0][0] return
    >undefined when there is only one element in the array but returns the
    >first element correctly when there are at least two elements?[/color]

    That is because the behaviour of the Array object is different
    when you pass two or more integers to the Array constructor.

    If you pass one integer to the Array constructor it will create
    an Array of that size. If you pass two or more integers it will
    use them as elements in the array.

    In your example,

    var temp = new Array(new Array(0))

    new Array( 0) creates an array of zero length.

    temp = new Array(new Array(0,1))

    new Array( 0,1) creates an array that contain [ 0, 1].




    Peace, Vm
    Yaz

    Providing complicated solutions to simple problems since 1997.

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Array indexing problem

      to heave chunks wrote:
      [color=blue][color=green]
      >>Consider the following javascript:
      >>
      >>var temp = new Array(new Array(0))
      >>document.writ eln(temp[0][0])
      >>
      >>temp = new Array(new Array(0,1))
      >>document.writ eln(temp[0][0])
      >>
      >>One would assume that it would print "0 0" that is the first elements
      >>of the arrays, but it prints "undefined 0". Why does temp[0][0] return
      >>undefined when there is only one element in the array but returns the
      >>first element correctly when there are at least two elements?[/color]
      >
      > That is because the behaviour of the Array object is different
      > when you pass two or more integers to the Array constructor.
      >
      > If you pass one integer to the Array constructor it will create
      > an Array of that size. If you pass two or more integers it will
      > use them as elements in the array.[/color]

      More, how it is interpreted depends on the implementation. To be
      sure, use Array literals:

      var temp = [[0]];

      creates an Array object with an Array object as only element which
      only element is zero and stores a reference to it in `temp'. The
      "same" can be accomplished with leaving out constructor arguments,
      but requiring another variable:

      var help = new Array();
      help[0] = 0;
      var temp = new Array(help);

      Note that although temp[0][0] retrieves `0', deleting or overwriting
      `help[0]' changes that value since the reference is stored as element,
      not the primitive value of zero:

      help[0] = 42;
      alert(temp.join (",")); // 42


      PointedEars

      P.S.
      Your `From' is borken, read and follow
      <http://www.interhack.n et/pubs/munging-harmful/>
      if you want to be read in the future.

      Comment

      Working...