We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,913 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Add an arraypart by a loop

Hey,

I'm trying to do a loop in javascript, that adds an array into a variable.
Because it is a bit hard to explain exactly what I want, please look at this code, which does the (for the beginning) same.

var s="one, two, three"; //this can be changed
var p=s.split(',');
var l=p.length;
if(l==1){fn(p[0]);}
if(l==2){fn(p[0],p[1]);}
if(l==3){fn(p[0],p[1],p[2]);}
if(l==4){fn(p[0],p[1],p[2],p[3]);}
if(l==5){fn(p[0],p[1],p[2],p[3],p[4]);}
......

All the if-statements should be replaced by a nice loop.
Anybody has an idea?

3
Contributors
10
Replies
8 Hours
Discussion Span
1 Year Ago
Last Updated
11
Views
trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
var p = "one, two, three".split(',')
;for(i in p)fn(p[i]);
Troy III
Master Poster
736 posts since Jun 2008
Reputation Points: 140
Solved Threads: 95
Skill Endorsements: 5

Like:

fn(p[0])
fn(p[0], p[1])
fn(p[0], p[1], p[2])...


not:

fn(p[0])
fn(p[1])
fn(p[2])...

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Does your existing code work? What does fn do?

Troy III's method should work.

I usually go with something less fancy (a plain old for-loop):

for (var i = 0; i < l; i++) {
   fn(p[i]);
 }
DavidB
Posting Whiz
307 posts since Jul 2006
Reputation Points: 48
Solved Threads: 16
Skill Endorsements: 16

fn calles a function, and the arrays are its parameters, so you can't call the function 5 times with an other first parameter, you have to add a new.
and yes, my code works, the problem is just it's limited and doesn't look nice...

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

So you are dynamically changing the number of parameters passed to a function?

Hmmnn. . . I have not seen that before. But that doesn't mean it is not possible. (I am not the world's best Javascript programmer.)

Off the top of my head, perhaps you could create a struct with all the parameters added before the function call. Then, in the function, extract the relevant data as required. Since only the address of a struct is passed into a function, its size doesn't have to be defined before program execution.

Or could you pass tree into the function and split it there?

Hopefully, somebody more knowledgeable than I with regards to Javascipt can suggest a more elegant solution.

DavidB
Posting Whiz
307 posts since Jul 2006
Reputation Points: 48
Solved Threads: 16
Skill Endorsements: 16

Like:

fn(p[0])
fn(p[0], p[1])
fn(p[0], p[1], p[2])...


not:

fn(p[0])
fn(p[1])
fn(p[2])...

well, conventionally -that's not doable.
you'll need to write your function on the fly

Troy III
Master Poster
736 posts since Jun 2008
Reputation Points: 140
Solved Threads: 95
Skill Endorsements: 5

OK, I'll stick with the if-statements.

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

how complicated is the fn function?

Troy III
Master Poster
736 posts since Jun 2008
Reputation Points: 140
Solved Threads: 95
Skill Endorsements: 5

the fn function can be everything. i want to do a function proo(), where you instead of calling a function directly call this function, which then calles your function.
So imagine, I want to call the function hello().
Instead of hello(); you say proo('hello','');
That's easy, it gets hard with parameters:

function hello(a,b){alert(a + b);} 
proo('hello', 'firstPara, secondPara');

Sounds probably stupid, but I need it.

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

my Math.average method may give you a hint on this...
snip is at:
http://www.daniweb.com/forums/attachment.php?attachmentid=6760&d=1216985919
discussion at
http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/136037/655952#post655952

A function written this way is self-adjustable to dyna-length of arguments given. This approach may solve your problem.

Troy III
Master Poster
736 posts since Jun 2008
Reputation Points: 140
Solved Threads: 95
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0878 seconds using 2.74MB