stepping threw an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abby Lee

    stepping threw an array

    I've created the following array and want to be able to process each
    indivisual object whenever my function is called. Is this best done
    with a "foreach"? I don't see a foreach in my javascript book...is
    there another way to step threw an array to process each item?

    var trvtps = new Array('autoReim b','airRail','c ar','taxi','lod ge','meals','mi sc');
  • Lasse Reichstein Nielsen

    #2
    Re: stepping threw an array

    abbylee26@hotma il.com (Abby Lee) writes:
    [color=blue]
    > I've created the following array and want to be able to process each
    > indivisual object whenever my function is called. Is this best done
    > with a "foreach"? I don't see a foreach in my javascript book...is
    > there another way to step threw an array to process each item?
    >
    > var trvtps = new Array('autoReim b','airRail','c ar','taxi','lod ge','meals','mi sc');[/color]

    There are two ways:
    ---
    for (var i = 0; i < trvtps.length; i++) {
    var trvtp = trvtps[i];
    // something with trvtp.
    }
    ---
    and
    ---
    for (var i in trvtps) {
    var trvtp = trvtps[i];
    // something with trvtp.
    }
    ---

    The former is targeted at an array (something with a length and
    integer indices) whereas the latter works on any object and
    iterates all enumerable properties, not only integer ones.

    As long as you haven't added properties to "Object.prototy pe",
    "Array.prototyp e" or "trvtps" itself, then only the integer properties
    are properties of the array.

    If you have an array where not all positions have a value, the
    for(...in...) will skip these positions, whereas the incrementing
    for loop will access all positions less than the length.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Lee

      #3
      Re: stepping threw an array

      Abby Lee said:[color=blue]
      >
      >I've created the following array and want to be able to process each
      >indivisual object whenever my function is called. Is this best done
      >with a "foreach"? I don't see a foreach in my javascript book...is
      >there another way to step threw an array to process each item?
      >
      >var trvtps = new
      >Array('autoRei mb','airRail',' car','taxi','lo dge','meals','m isc');[/color]

      The length attribute of the array tells you how many entries
      it contains. They will be numbered from 0 to length-1.

      for(var i=0;i<trvtps.le ngth;i++){
      alert(trvtps[i]);
      }

      Comment

      • Mick White

        #4
        Re: stepping threw an array

        Abby Lee wrote:
        [color=blue]
        > I've created the following array and want to be able to process each
        > indivisual object whenever my function is called. Is this best done
        > with a "foreach"? I don't see a foreach in my javascript book...is
        > there another way to step threw an array to process each item?
        >
        > var trvtps = new Array('autoReim b','airRail','c ar','taxi','lod ge','meals','mi sc');[/color]
        t=trvtps.length ;
        while(t--){myProcessFunc tion(trvtps[t])}
        Mick

        Comment

        • Evertjan.

          #5
          Re: stepping threw an array

          Mick White wrote on 09 sep 2004 in comp.lang.javas cript:
          [color=blue]
          > t=trvtps.length ;
          > while(t--){myProcessFunc tion(trvtps[t])}[/color]

          var t=0;
          while(t<trvtps. length)
          myProcessFuncti on(trvtps[t++])


          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress,
          but let us keep the discussions in the newsgroup)

          Comment

          • Mick White

            #6
            Re: stepping threw an array

            Evertjan. wrote:
            [color=blue]
            > Mick White wrote on 09 sep 2004 in comp.lang.javas cript:
            >[color=green]
            >>t=trvtps.leng th;
            >>while(t--){myProcessFunc tion(trvtps[t])}[/color][/color]
            [color=blue]
            > var t=0;
            > while(t<trvtps. length)
            > myProcessFuncti on(trvtps[t++])
            >[/color]
            Both approaches work equally well. Did you try both?
            Mick

            Comment

            • Evertjan.

              #7
              Re: stepping threw an array

              Mick White wrote on 09 sep 2004 in comp.lang.javas cript:
              [color=blue]
              > Evertjan. wrote:
              >[color=green]
              >> Mick White wrote on 09 sep 2004 in comp.lang.javas cript:
              >>[color=darkred]
              >>>t=trvtps.len gth;
              >>>while(t--){myProcessFunc tion(trvtps[t])}[/color][/color]
              >[color=green]
              >> var t=0;
              >> while(t<trvtps. length)
              >> myProcessFuncti on(trvtps[t++])
              >>[/color]
              > Both approaches work equally well. Did you try both?[/color]

              Who are you asking, Mick?

              I just like to have my pointers increasing and minimizing superfluous ()


              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress,
              but let us keep the discussions in the newsgroup)

              Comment

              Working...