473,811 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Jul 23 '05 #1
4 25393
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*****@somedo main.cam> writes:
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']; ....
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.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).
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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
"Csaba Gabor" <cs***@z6.com > wrote in message
news:Sc******** ***********@new s.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
7208
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 foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
14
1812
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) and have it sequentially enumerate through both of them.
11
1297
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 has the usual methods: this, Add, Insert, Remove and Contains. There are no differences between this class and the other generated classes I'm using other than the type of the contained object. The other classes can be foreach'ed quite happily.
8
2110
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 case with a regular for loop when the second expression is false when the loop begins). Is there an explanation for this?
1
2601
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) equivalent in MC++? Thanks
15
21552
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
2137
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
2281
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 matrices are sparse by design/definition in PHP. I'm doing some matrix manipulation in a Finite Element program. Translating Fortran to PHP, because hosters won't allow anything else than PHP. I wish PHP would do array and matrix stuff like Fortran...
8
273
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 other words that could fill in the blank though. "Or am I _______ as usual?"
0
9728
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10135
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9205
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.