Invision Power Services: javascript foreach equivalent? - Invision Power Services

Jump to content

Web Design and Coding

Our community chat forum areas are for off-topic discussion only. Please do not post topics about IPS or its products and services here. We provide other forum areas for IPS discussion.

Do you need technical support on IPS products or services?

You can obtain support via the client area, or you can try to obtain peer-to-peer support at IPS Resources.

Did you find a bug in one of our products?

If you believe you've found a bug please post it to the bug tracker.

Have a suggestion or feedback?

Use the company feedback forum or appropriate product feedback forum. You can also submit a ticket in your client area if it's a private matter.
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

javascript foreach equivalent? Rate Topic: -----

#1 User is offline   Trel Icon

  • ¯\(°_o)/¯
  • Icon
  • View blog
  • Group: +Active Customers
  • Posts: 3,416
  • Joined: 05-November 02

Posted 07 May 2005 - 05:06 AM

how can I setup a foreach type structure in Javascript?
Posted Image
0

#2 User is offline   Michael K. Icon

  • Advanced Member
  • PipPipPipPip
  • View blog
  • Group: Members
  • Posts: 424
  • Joined: 31-August 03

Posted 07 May 2005 - 08:14 AM

var ar = new Array()
  ar[0] = 20
  ar[1] = 50
  
  for(i=0;i<ar.length;i++) {
	alert(ar[i])
  }


Something like that?
Michael
"When life gives you lemons, find someone with a papercut."
0

#3 User is offline   Nash12 Icon

  • Spam Happy
  • PipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 591
  • Joined: 02-June 04
  • Location:Arkham Asylum

Posted 07 May 2005 - 09:28 AM

Unlike PHP there is no foreach-construct in Javascript, only "for" as Gornakle showed you. :)
0

#4 User is offline   justsomeguy Icon

  • IPB Newbie
  • Pip
  • View blog
  • Group: Members
  • Posts: 1
  • Joined: 25-January 06

Posted 25 January 2006 - 10:13 PM

View PostNash12, on May 7 2005, 04:28 AM, said:

Unlike PHP there is no foreach-construct in Javascript, only "for" as Gornakle showed you. :)


Actually that's not true at all.
As all javascript objects are really just associative arrays, there is a foreach like syntax for the 'for' construct.
Without it it would be very hard to work with many common javascript objects.
It's actually very simple to use and incredibly useful if you like associative arrays.

You just have to make sure to use it on an associative array and not an object.
Example below

Good (assuming Object has not been overloaded or extended):
var array = new Object(); //this is safe only if you can assure object has not been extended.
//use the below if Object has been extended
var array;

//everything below here works fine regardless of the two above cases
array['something'] = 'yay';
array['somethingelse'] = 'more';

for ( keyVar in array ) {
   alert(array[keyVar]);
}



Bad:
var array = new Array(); //uh oh, array is an object which means it's an associative array

//the below loop will actually give you results, despite never putting "values" into
//the array variable. This is because array has functions defined from the Array() object
//which will be grabbed by the keyVar since objects are really associative arrays
for ( keyVar in array ) {
   alert(array[keyVar]);
}


So there you go, and it's incredibly useful. :)

~Some random guy on the internet
0

#5 User is offline   Trel Icon

  • ¯\(°_o)/¯
  • Icon
  • View blog
  • Group: +Active Customers
  • Posts: 3,416
  • Joined: 05-November 02

Posted 28 January 2006 - 04:21 PM

thanks =D
Posted Image
0

#6 User is offline   Luke Icon

  • Too detail oriented
  • Icon
  • View blog
  • Group: +Active Customers
  • Posts: 5,475
  • Joined: 14-August 04
  • Location:USA

Posted 28 January 2006 - 04:55 PM

Dont you have your "Good" and "Bad" backwards ;)?
Luke
0

#7 User is offline   _ Icon

  • Needs Serious Help
  • PipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 1,655
  • Joined: 09-December 03

Posted 28 January 2006 - 06:18 PM

for(a in b) { ... }

Posted Image
(colours are based on your IP address and almost* unique)
(* meaning that two bytes are left unused, and there's always the chance of a collision)
0

#8 User is offline   hextreme Icon

  • IPB Newbie
  • Pip
  • View blog
  • Group: Members
  • Posts: 1
  • Joined: 24-April 06

Posted 24 April 2006 - 06:22 PM

View Postjustsomeguy, on Jan 25 2006, 11:13 PM, said:

Actually that's not true at all.
As all javascript objects are really just associative arrays, there is a foreach like syntax for the 'for' construct.


Thanks. And now for something a bit harder: is it possible to use a 'for' statement for listing all the field names/values in a form?
0

#9 User is offline   JeremyToo Icon

  • IPB Newbie
  • Pip
  • View blog
  • Group: Members
  • Posts: 1
  • Joined: 06-May 06

Posted 06 May 2006 - 02:26 PM

View Posthextreme, on Apr 24 2006, 06:22 PM, said:

Thanks. And now for something a bit harder: is it possible to use a 'for' statement for listing all the field names/values in a form?


I use it all the time. Check it out:

<script>
function showVals() {
theForm = document.getElementById('testing');
theStr = '';
for (i=0; i < theForm.elements.length; i++) {
ele = theForm.elements[i];
theStr += ele.name + ' : ' + ele.value + "\n";
}
alert (theStr);
}
</script>


<form id="testing">
<input type="text" name="namers" value="Namers Test!"/>
<input type="text" name="password" value="Password Test!"/>
<input type="button" onclick="showVals(); return false;" name="button" value="Display Values"/>
</form>
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users