473,396 Members | 1,765 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,396 software developers and data experts.

foreach() in Javascript?

How do I create an array in JavaScript and then go through it?

I want to feed a function with the string "foo,bar,rab,oof" and want JavaScript
to do something with each line, like this (in pseudo-code):

myfunc("foo,bar,rab,oof");

function myfunc(string){
foreach i (split(",", string)){
alert(i);
}
}

Or something like that.

--
Sandman[.net]
Jul 20 '05 #1
4 77243


Sandman wrote:
How do I create an array in JavaScript and then go through it?
var a = [1, 2, 3, 4, 5];
for (var i = 0; i < a.length; i++)
alert(a[i])
I want to feed a function with the string "foo,bar,rab,oof" and want JavaScript
to do something with each line, like this (in pseudo-code):

myfunc("foo,bar,rab,oof");
I don't see any lines there, it is a string literal.
function myfunc(string){
foreach i (split(",", string)){
alert(i);
}
}


function myfunc(string){
var a = string.split(',');
for (var i = 0; i < a.length; i++) {
alert(a[i]);
}
}

myfunc("foo,bar,rab,oof");

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Sandman wrote:
How do I create an array in JavaScript and then go through it?

I want to feed a function with the string "foo,bar,rab,oof" and want JavaScript
to do something with each line, like this (in pseudo-code):

myfunc("foo,bar,rab,oof");

function myfunc(string){
foreach i (split(",", string)){
alert(i);
}
}


I believe the construct you may be looking for is, or at least one that
should get you started, is: "for ... in"

Docs and an example can be found at:

http://devedge.netscape.com/library/...t.html#1004815
Jul 20 '05 #3
Sandman <mr@sandman.net> writes:
myfunc("foo,bar,rab,oof");

function myfunc(string){
foreach i (split(",", string)){
alert(i);
}
}


Very close:

function myfunc(string) {
var arr = string.split(",");
for (var i in arr) {
alert(arr[i]);
}
}

/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 20 '05 #4
In article <4q**********@hotpop.com>,
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote:
Sandman <mr@sandman.net> writes:
myfunc("foo,bar,rab,oof");

function myfunc(string){
foreach i (split(",", string)){
alert(i);
}
}


Very close:

function myfunc(string) {
var arr = string.split(",");
for (var i in arr) {
alert(arr[i]);
}
}


Thanks!

--
Sandman[.net]
Jul 20 '05 #5

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

Similar topics

3
by: MicroMoth | last post by:
Hi, I'm trying to call a Javascript function within a foreach loop. I am loop over a series of users and I want to call the JS function which opens a new window, passing in the user id to each...
1
by: JumpingOffPlace | last post by:
Hi, I'm hoping that the wealth of knowledge here can stop me from spinning my wheels on this syntax error for hours. :) Below is the code, and the error I am recieving.... Code: <!DOCTYPE...
4
by: dannyboy | last post by:
Hi I'm trying to echo some php into a piece of javascript that checks that the selectedIndex in a given select name is not 0, and gives an alert if this is so: //// <script...
0
by: rpjd | last post by:
Apache2 over XP Home, PHP5, PostgreSQL8.2 If this is not the correct forum for this, it can be reposted accordingly. I have php variables/arrays that I want to display in a php webpage. What I am...
6
by: adamscybot | last post by:
Here is the site in question: http://www.sws.vxcomputers.com/h2k/ Basically, on the left, you'll see a roster (them images) and you have to click the "Counter-Strike" link for it to load the...
14
by: rashgang | last post by:
i have dynamically created checkbox which will like yahoo mail if check box selected it will highlight a row but deleteall function is not working plz chek it function doDeleteAll($objArray){ ...
21
by: rashgang | last post by:
no check box selected when i gave delete all link the error is coming <?php include "includes/connection.php"; include "includes/Functions_category.php"; include...
1
by: Constantine AI | last post by:
Here is the situation i am currently trying to update a sales order using PHP and javascript. When you click on the edit button it brings you a new site page with existing sales order details within....
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
1
by: mahhood | last post by:
Here is the complete code. <?php // ****: holder.php - v. 5.03 // PRELIMINARIES: // Get Includes:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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...

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.