473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

foreach equivalent

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

document.getElementById('header').style['background']=default.header['background'];
document.getElementById('header').style['borderColor']=default.header['borderColor'];
document.getElementById('header').style['borderWidth']=default.header['borderWidth'];
document.getElementById('middle').style['background']=default.middle['background'];
document.getElementById('middle').style['borderColor']=default.middle['borderColor'];
document.getElementById('middle').style['borderWidth']=default.middle['borderWidth'];
document.getElementById('footer').style['background']=default.footer['background'];
document.getElementById('footer').style['borderColor']=default.footer['borderColor'];
document.getElementById('footer').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($default as $segment) {
foreach($segment as $element=>$value) {
document.getElementById($segment).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?
Jul 23 '05 #1
4 25377
Tony wrote:
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.


var aSettings = {background : "black",
borderColor: "green"}
for (var idx in aSettings)
default.header[idx] = aSettings[idx];
Csaba Gabor from Vienna
Jul 23 '05 #2
"Tony" <so*****@somedomain.cam> writes:
I'm trying to simplify a list of statements similar to the following:

document.getElementById('header').style['background']=default.header['background'];
document.getElementById('header').style['borderColor']=default.header['borderColor']; ....
In PHP, I could handle this fairly easily by defining an array and stepping
through with nested foreach loops:
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.getElementById(segment); // 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).
Is there a comparable way to accomplish this in JavaScript?
Yep.
A related question is: Is there a simpler way to define the arrays than by
defining: ....

Yep.

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


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

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
"Csaba Gabor" <cs***@z6.com> wrote in message
news:Sc*******************@news.chello.at...
Tony wrote:
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.


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


Thanks - that will simplify the task considerably!
Jul 23 '05 #4
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com...
I've done a bit of searching but I can't find a construct that will
simplify
this to any great degree. Any suggestions?


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


I'm checking that out now!

Thanks for the help with the code.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
14
by: Josh Ferguson | last post by:
I don't believe a syntax driven equivalent exists for this, but I just thought it would be neat if you could use foreach to do something like this: foreach (Object x in collection1, collection2)...
11
by: Flinchvoid | last post by:
Interesting little problem; go easy on me because I'm just a humble scripter turned c# developer. I've a class UserList which I auto-generated with a python script, it extends CollectionBase and...
8
by: Brad Wood | last post by:
When I do this: foreach( Button btn in myForm.Controls ) An exception is raised when no buttons exist. I would simply expect execution to continue after the foreach loop (just as would be the...
1
by: GeRmIc | last post by:
Hi, I am working on interop using C and managed c++ (on Visual Studio 2003 IDE). How do i simulate the foreach loop present in C# or VB.NET in MC++ foreach (DataRow dr in ds.Tables.Rows)...
15
by: Fabio Cannizzo | last post by:
Is it possible to do something similar to the STL iterators, i.e. to execute a foreach loop in reverse order? Thanks, Fabio
8
by: cordmcphail | last post by:
Hello, Is there a similar function in JS as the PHP foreach() loop? PHP Ex. foreach($something as $key => $value) { execute code here; } I have created an object in JS:
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
8
by: Bill Butler | last post by:
"raylopez99" <raylopez99@yahoo.comwrote in message news:bd59f62a-5b54-49e8-9872-ed9aef676049@t54g2000hsg.googlegroups.com... <snip> I don't think "right" is the correct word. There are many...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.