for in loop. reverse traverse

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R. Rajesh Jeba Anbiah

    for in loop. reverse traverse

    Unfortunately, I couldn't find any way to traverse the object array in
    reverse order. I'd thought there must be a way to do it with for..in
    loop, but couldn't find anything yet. Could someone please help me?
    TIA.

    --
    <?php echo 'Just another PHP saint'; ?>
    Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

  • Michael Winter

    #2
    Re: for in loop. reverse traverse

    On 9 Dec 2004 11:17:15 -0800, R. Rajesh Jeba Anbiah
    <ng4rrjanbiah@r ediffmail.com> wrote:
    [color=blue]
    > Unfortunately, I couldn't find any way to traverse the object array in
    > reverse order.[/color]

    What's an object array?
    [color=blue]
    > I'd thought there must be a way to do it with for..in loop, but couldn't
    > find anything yet.[/color]

    The for..in statement doesn't return values in any particular order so
    reversing is not possible.

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Joakim Braun

      #3
      Re: for in loop. reverse traverse

      "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@r ediffmail.com> skrev i meddelandet
      news:1102619835 .408631.286170@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > Unfortunately, I couldn't find any way to traverse the object array in
      > reverse order. I'd thought there must be a way to do it with for..in
      > loop, but couldn't find anything yet. Could someone please help me?
      > TIA.
      >
      > --
      > <?php echo 'Just another PHP saint'; ?>
      > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/[/color]

      A probably meaningless hack would be to do a for...in with the object and
      save the property names in an array. Then traverse that array backwards and
      access the named properties of the first object.

      Joakim Braun


      Comment

      • Grant Wagner

        #4
        Re: for in loop. reverse traverse

        comp.lang.javas cript FAQ - http://jibbering.com/faq
        "Joakim Braun" <joakim.braun@j fbraun.removeth is.com> wrote in message
        news:e74ud.3043 $Of5.2157@nntps erver.swip.net. ..[color=blue]
        > "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@r ediffmail.com> skrev i[/color]
        meddelandet[color=blue]
        > news:1102619835 .408631.286170@ z14g2000cwz.goo glegroups.com.. .[color=green]
        > > Unfortunately, I couldn't find any way to traverse the object array[/color][/color]
        in[color=blue][color=green]
        > > reverse order. I'd thought there must be a way to do it with for..in
        > > loop, but couldn't find anything yet. Could someone please help me?
        > > TIA.
        > >
        > > --
        > > <?php echo 'Just another PHP saint'; ?>
        > > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/[/color]
        >
        > A probably meaningless hack would be to do a for...in with the object[/color]
        and[color=blue]
        > save the property names in an array. Then traverse that array[/color]
        backwards and[color=blue]
        > access the named properties of the first object.[/color]

        But the values retrieved with for...in aren't guaranteed to be in any
        particular order in the first place, so "reverse order" has no meaning,
        since you can't be sure what the "forward order" will be.

        If you want to store key/value pairs and be guaranteed of their order,
        you will need to store both the key/value pair and the order they are
        added in:

        function MyOrderedMap() {
        var keys = [];
        var values = [];

        this.add = function(key, value) {
        // test to make sure 'key' isn't in 'keys'
        var ii = keys.length;
        while (ii-- > 0) {
        if (key == keys[ii]) {
        return false;
        }
        }
        keys.push(key);
        values.push(val ues);
        return true;
        }
        this.getKeysInR everseOrder = function() {
        return keys.reverse();
        // or return keys.reverse(). join(',');
        // or whatever you want
        }
        }

        You probably want methods for getting a value when passed a key:

        this.getValue = function(key) {
        var ii = keys.length;
        while (ii-- > 0) {
        if (key == keys[ii]) {
        return values[ii];
        }
        }
        return null;
        }

        and other methods as well to clear the list, etc.

        I threw this together in a hurry, there may be syntax errors.

        --
        Grant Wagner <gwagner@agrico reunited.com>


        Comment

        Working...