foreach() in Javascript?

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

    foreach() in Javascript?

    How do I create an array in JavaScript and then go through it?

    I want to feed a function with the string "foo,bar,rab,oo f" and want JavaScript
    to do something with each line, like this (in pseudo-code):

    myfunc("foo,bar ,rab,oof");

    function myfunc(string){
    foreach i (split(",", string)){
    alert(i);
    }
    }

    Or something like that.

    --
    Sandman[.net]
  • Martin Honnen

    #2
    Re: foreach() in Javascript?



    Sandman wrote:
    [color=blue]
    > How do I create an array in JavaScript and then go through it?[/color]

    var a = [1, 2, 3, 4, 5];
    for (var i = 0; i < a.length; i++)
    alert(a[i])
    [color=blue]
    > I want to feed a function with the string "foo,bar,rab,oo f" and want JavaScript
    > to do something with each line, like this (in pseudo-code):
    >
    > myfunc("foo,bar ,rab,oof");[/color]

    I don't see any lines there, it is a string literal.
    [color=blue]
    > function myfunc(string){
    > foreach i (split(",", string)){
    > alert(i);
    > }
    > }[/color]

    function myfunc(string){
    var a = string.split(', ');
    for (var i = 0; i < a.length; i++) {
    alert(a[i]);
    }
    }

    myfunc("foo,bar ,rab,oof");

    --

    Martin Honnen


    Comment

    • George Jempty

      #3
      Re: foreach() in Javascript?

      Sandman wrote:[color=blue]
      > How do I create an array in JavaScript and then go through it?
      >
      > I want to feed a function with the string "foo,bar,rab,oo f" and want JavaScript
      > to do something with each line, like this (in pseudo-code):
      >
      > myfunc("foo,bar ,rab,oof");
      >
      > function myfunc(string){
      > foreach i (split(",", string)){
      > alert(i);
      > }
      > }[/color]

      I believe the construct you may be looking for is, or at least one that
      should get you started, is: "for ... in"

      Docs and an example can be found at:


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: foreach() in Javascript?

        Sandman <mr@sandman.net > writes:
        [color=blue]
        > myfunc("foo,bar ,rab,oof");
        >
        > function myfunc(string){
        > foreach i (split(",", string)){
        > alert(i);
        > }
        > }[/color]

        Very close:

        function myfunc(string) {
        var arr = string.split(", ");
        for (var i in arr) {
        alert(arr[i]);
        }
        }

        /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

        • Sandman

          #5
          Re: foreach() in Javascript?

          In article <4qt76snh.fsf@h otpop.com>,
          Lasse Reichstein Nielsen <lrn@hotpop.com > wrote:
          [color=blue]
          > Sandman <mr@sandman.net > writes:
          >[color=green]
          > > myfunc("foo,bar ,rab,oof");
          > >
          > > function myfunc(string){
          > > foreach i (split(",", string)){
          > > alert(i);
          > > }
          > > }[/color]
          >
          > Very close:
          >
          > function myfunc(string) {
          > var arr = string.split(", ");
          > for (var i in arr) {
          > alert(arr[i]);
          > }
          > }[/color]

          Thanks!

          --
          Sandman[.net]

          Comment

          Working...