artfuldev
artfuldev

Reputation: 1118

How to pass an array as argument to a function in javascript?

I'm trying to make helper functions to make use of the Google Analytics API, and I have a simple problem building strings. The scenario is, I have to apply a filter, and there may be n number of filters (a nominal amount, not more than 128 anyhow). I wanted to write a function which can take in the n strings and combine them with comma-separation in between.

I don't know if the number of arguments can be variable in javascript, and if it can take arrays as arguments in javascript (I am a newbie to JS), but I see no difference as variables are simply var and there is no datatype anywhere in JS (I come from a C++/Java background and find it confusing as it is). So I tried passing an array as an argument to a function so that the no. of things I can work with can be dynamic, decided by the elements in the array.

When I started searching for solutions, I came across this page. After that I recently came across this thread which also refers the same link and the format they have provided does me no good.

For the sake of clarity, let me provide the function definition I've written.

/**
 * Utility method to build a comma-ed string from an array of strings
 * for the multiple-condition requests to the GA API
 */
function buildString(strArray)
{
    var returnString='';
    for(var x in strArray)
        returnString+=x+',';
    return returnString = returnString.substring(0, returnString.length - 1);
}

And this is how I call it:

buildString.apply(this,[desc(visits),source])

where desc(visits) and source are both strings, so I assumed I'm sending an array of strings. Strangely, both this and null in the apply() call to the buildString function give me "0,1" as the return value.

Please tell me where I'm going wrong. Am I passing the array in a wrong manner? Or is my function definition wrong? Or is there some other simpler way to achieve what I'm trying?

Upvotes: 1

Views: 27543

Answers (4)

jingyinggong
jingyinggong

Reputation: 646

Js argument can be any type and no limit to the number of argument,

But it is recommanded use 3-4 arguments at most, if there are more args, you can pass it as an object or array.

You don't need to worry about the type of args, js will do the job.

For example:

var func1 = function(a) {
    console.log(a);
}

func1('good');
func1(1);
func1(['good', 'a', 1]);
func1({name: 'fn1', age: 12});

Anything you like!,

You can even define a function with three arguments, but only pass one is ok!

var func2 = function(a, b, c) {
    console.log(a);
}

func2(1);
func2(1, 'good');
func2(1, 'good', 'night', 4);

And default array obj has many build-in func; for example:

var arr = ['good', 'night', 'foo', 'bar']; //define any thing in a array
str = arr.join(','); //you may get 'good,night,foo,bar'
var arr1 = str.split(','); // you may get ['good', 'night', 'foo', 'bar'];

Upvotes: 0

Barney
Barney

Reputation: 16466

You're over complicating things — JavaScript arrays have a built-in join method:

[ desc( visits ), source ].join( ',' );

EDIT: simpler still: the toString method:

[ desc( visits ), source ].toString();

Upvotes: 2

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18354

The easiest would be to use the built-in join method:

[desc(visits), source].join(',');

Anyway, your problem was in the for..in loop

Instead of this:

for(var x in strArray){
    returnString+=x+',';
}

You should have:

for(var i in strArray){
    var x = strArray[i]; //Note this
    returnString+=x+',';
}

Because for...in gives back the index/key, not the actual element as foreach does in other languages

Also your call should be:

buildString.call(this,[desc(visits),source]) or just buildString([desc(visits),source])

Cheers

Upvotes: 0

Barmar
Barmar

Reputation: 782785

Passing arrays to functions is no different from passing any other type:

var string = buildString([desc(visits), source]);

However, your function is not necessary, since Javascript has a built-in function for concatenating array elements with a delimiter:

var string = someArray.join(',');

Upvotes: 5

Related Questions