473,406 Members | 2,710 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,406 software developers and data experts.

JS equiv for PHP foreach()

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

Feb 14 '06 #1
8 2112
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]);
}

Feb 14 '06 #2


co*********@gmail.com wrote:

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[i] 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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 14 '06 #3
Thank you both very much. This is what I needed to know!

Cmac

Feb 14 '06 #4
Martin Honnen wrote:
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
Feb 14 '06 #5
VK

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


<http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6>

Feb 14 '06 #6
VK wrote:
Thomas 'PointedEars' Lahn wrote:
Nice to know about `for each' in JavaScript 1.6, though. Where did you
find it?


<http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6>


I do not see a `for each' statement described there or in any of the linked
resources.
PointedEars
Feb 14 '06 #7
Thomas 'PointedEars' Lahn wrote:
VK wrote:
Thomas 'PointedEars' Lahn wrote:
Nice to know about `for each' in JavaScript 1.6, though. Where did you
find it?


<http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6>


I do not see a `for each' statement described there or in any of the
linked resources.


One needs to follow the "Discussion" link:

<URL:http://developer.mozilla.org/en/docs/Talk:New_in_JavaScript_1.6>
PointedEars
Feb 14 '06 #8
Thomas 'PointedEars' Lahn wrote:
Martin Honnen wrote:
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.


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
Feb 14 '06 #9

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

Similar topics

8
by: Nige | last post by:
I've got an old web site that comes up before its replacement in Google listings. I've read the document on Google about sending a "301" code from the server:...
10
by: Newry | last post by:
Hi, If you have both this HTTP header: Content-Type: text/html; charset=ISO-8859-1 and this HTML element in the <head>: <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
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...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
0
dmjpro
by: dmjpro | last post by:
i use a code ..... in my html page like .... <meta http-equiv = 'Window-target' content = '_blank'> what will be the effect .... what i know about the http-equiv = 'Window-target' ......
2
by: runway27 | last post by:
i have a registration page which is a self submitting form <form action="<?php echo $_SERVER; ?>" method="POST" id="test2" name="registrationform"> where in a user fill a form, after the data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.