JS equiv for PHP foreach()

C

cordmcphail

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:
var ibm = new Object();

ibm['0'] = 240;
ibm['1'] = 241;
ibm['2'] = 242;
ibm['3'] = 243;
etc...

and I want to access it as
if($key == $somthing)
{
$this = $this;
}

Any help appreciated!

Cmac
 
S

somejeff

To iterate through the properties of an object, you use for(variable in
object) {}
for example:

for(var x in ibm) {
alert(x + "=" + ibm[x]);
}
 
M

Martin Honnen

Is there a similar function in JS as the PHP foreach() loop?
I have created an object in JS:
var ibm = new Object();

ibm['0'] = 240;
ibm['1'] = 241;
ibm['2'] = 242;
ibm['3'] = 243;

Looks more like you want an array e.g.
var ibm = new Array();
imb[0] = 240;
ibm[1] = 241;
ibm[2] = 242;
ibm[3] = 243;
or shorter
var ibm = [ 240, 241, 242, 243 ];

If you use an array then stick with
for (var i = 0, l = ibm.length; i < l; i++) {
// use i and/or ibm here
}

If you really want an object then for..in allows you to enumerate the
enumerable properties of the object e.g.

var ibm = new Object();

ibm['0'] = 240;
ibm['1'] = 241;
ibm['2'] = 242;
ibm['3'] = 243;

for (var key in ibm) {
// use key and/or imb[key] here
}

JavaScript 1.6 (only supported in Mozilla 1.8 respectively Firefox 1.5
currently) also has for each..in to enumerate the property values of
enumerable properties e.g.

for each (var value in ibm) {
alert(value);
}


Take note that both for..in and for each..in will enumerate properties
that are 'inherited', e.g. if you have

Object.prototype.god = 'Kibo';

var ibm = new Object();

ibm['0'] = 240;

for each (var value in ibm) {
alert(value);
}

then two values are enumerated.
 
T

Thomas 'PointedEars' Lahn

Martin said:
If you really want an object then for..in allows you to enumerate the
enumerable properties of the object e.g.

var ibm = new Object();

ibm['0'] = 240; ^^^
ibm['1'] = 241;
ibm['2'] = 242;
ibm['3'] = 243;

for (var key in ibm) {
// use key and/or imb[key] here
}

It is not necessary to use string indexes here, though.
JavaScript 1.6 (only supported in Mozilla 1.8 respectively Firefox 1.5
currently) also has for each..in to enumerate the property values of
enumerable properties e.g.

for each (var value in ibm) {
alert(value);
}

The order in which the property values are retrieved is undefined at least
for the former access method. In PHP with `foreach', the elements are
retrieved in definition order.

Nice to know about `for each' in JavaScript 1.6, though. Where did you find
it?


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
The order in which the property values are retrieved is undefined at
least for the former access method.

Since the latter access method is a feature of E4X (ECMA-357, 12.3) and
`ibm' would not refer to a value of type XML or XMLList, the order in which
the property values are retrieved here is undefined, too, so it is also no
full equivalent of PHP's `foreach':
In PHP with `foreach', the elements are retrieved in definition order.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top