foreach equivalent

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

    #1

    foreach equivalent

    I'm trying to simplify a list of statements similar to the following:

    document.getEle mentById('heade r').style['background']=default.header['background'];
    document.getEle mentById('heade r').style['borderColor']=default.header['borderColor'];
    document.getEle mentById('heade r').style['borderWidth']=default.header['borderWidth'];
    document.getEle mentById('middl e').style['background']=default.middle['background'];
    document.getEle mentById('middl e').style['borderColor']=default.middle['borderColor'];
    document.getEle mentById('middl e').style['borderWidth']=default.middle['borderWidth'];
    document.getEle mentById('foote r').style['background']=default.footer['background'];
    document.getEle mentById('foote r').style['borderColor']=default.footer['borderColor'];
    document.getEle mentById('foote r').style['borderWidth']=default.footer['borderWidth'];


    In PHP, I could handle this fairly easily by defining an array and stepping
    through with nested foreach loops:

    $default = array(
    "header"=>array ( "background"=>" black", "borderColor"=> "green",
    "borderWidth"=> "5px" )
    "middle"=>array ( "background"=>" blue", "borderColor"=> "white",
    "borderWidth"=> "15px" )
    "footer"=>array ( "background"=>" red", "borderColor"=> "maroon",
    "borderWidth"=> "10px" )
    );

    foreach($defaul t as $segment) {
    foreach($segmen t as $element=>$valu e) {
    document.getEle mentById($segme nt).style['$element']=default[$segment][$element];
    }
    }

    (code is untested, just for demo)

    Is there a comparable way to accomplish this in JavaScript?

    A related question is: Is there a simpler way to define the arrays than by
    defining:
    default.header['background']="black";
    default.header['borderColor']="green";
    etc.

    I'm dealing with a situation where I have to define a large number of
    properties for a large number of segments, and be able to change them all
    dynamically.

    I've done a bit of searching but I can't find a construct that will simplify
    this to any great degree. Any suggestions?


  • Csaba Gabor

    #2
    Re: foreach equivalent

    Tony wrote:[color=blue]
    > A related question is: Is there a simpler way to define the arrays than by
    > defining:
    > default.header['background']="black";
    > default.header['borderColor']="green";
    > etc.[/color]

    var aSettings = {background : "black",
    borderColor: "green"}
    for (var idx in aSettings)
    default.header[idx] = aSettings[idx];


    Csaba Gabor from Vienna

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: foreach equivalent

      "Tony" <someone@somedo main.cam> writes:
      [color=blue]
      > I'm trying to simplify a list of statements similar to the following:
      >
      > document.getEle mentById('heade r').style['background']=default.header['background'];
      > document.getEle mentById('heade r').style['borderColor']=default.header['borderColor'];[/color]
      ....
      [color=blue]
      > In PHP, I could handle this fairly easily by defining an array and stepping
      > through with nested foreach loops:[/color]

      A similar structure in Javascript would be:

      var default = {
      header: { background: "black", borderColor: "green", borderWidth: "5px" },
      middle: { background: "blue", borderColor: "white", borderWidth: "15px" },
      footer: { background: "red", borderColor: "maroon", borderWidth: "10px" }
      };

      And the loop would be:

      for (var segment in default) {
      var elem = document.getEle mentById(segmen t); // fetch once!
      var properties = default[segment];
      for (var property in properties) {
      elem.style[property] = properties[property];
      }
      }

      The main difference between the PHP and Javascript for-each statements
      is that PHP iterates over the values (or key *and* value at the same
      time?) where Javascript always iterates over the keys only (if I read
      your PHP code correctly).
      [color=blue]
      > Is there a comparable way to accomplish this in JavaScript?[/color]

      Yep.
      [color=blue]
      > A related question is: Is there a simpler way to define the arrays than by
      > defining:[/color]
      ....

      Yep.

      [color=blue]
      > I've done a bit of searching but I can't find a construct that will simplify
      > this to any great degree. Any suggestions?[/color]

      Object literals. So smart they have their own name now :)
      <URL:http://www.json.org/>

      /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

      • Tony

        #4
        Re: foreach equivalent

        "Csaba Gabor" <csaba@z6.com > wrote in message
        news:Scoae.1594 1$0z2.11966@new s.chello.at...[color=blue]
        > Tony wrote:[color=green]
        >> A related question is: Is there a simpler way to define the arrays than
        >> by defining:
        >> default.header['background']="black";
        >> default.header['borderColor']="green";
        >> etc.[/color]
        >
        > var aSettings = {background : "black",
        > borderColor: "green"}
        > for (var idx in aSettings)
        > default.header[idx] = aSettings[idx];
        >
        >
        > Csaba Gabor from Vienna[/color]

        Thanks - that will simplify the task considerably!


        Comment

        • Tony

          #5
          Re: foreach equivalent

          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:oec5lmz2.f sf@hotpop.com.. .[color=blue][color=green]
          >> I've done a bit of searching but I can't find a construct that will
          >> simplify
          >> this to any great degree. Any suggestions?[/color]
          >
          > Object literals. So smart they have their own name now :)
          > <URL:http://www.json.org/>[/color]

          I'm checking that out now!

          Thanks for the help with the code.


          Comment

          Working...