1

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?

5
  • For variable parameter check stackoverflow.com/questions/2141520/… and you can pass an array or any type of object as a parameter in JS. Commented Jan 6, 2014 at 6:16
  • If you want to turn an array into a comma-separated string, use array.join(','). Commented Jan 6, 2014 at 6:17
  • The way you use your function is simply buildString(someArray). Commented Jan 6, 2014 at 6:18
  • buildString(someArray) also give the same returnString "0,1" Commented Jan 6, 2014 at 6:21
  • Thanks for the array.join(','). Didn't know that. :) Commented Jan 6, 2014 at 6:22

4 Answers 4

5

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(',');
Sign up to request clarification or add additional context in comments.

Comments

2

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();

Comments

0

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

4 Comments

The loop wasn't the only problem: OP was calling the function with .apply(), which takes the elements in the array and passes them as individual arguments.
Thanks for the clarification. So .apply() passes the elements of the array as individual arguments, which was not what I wanted.
Hi Kenshin. I updated the answer for not using apply, or using call
And thank you for pointing out the error in my for loop anyway. Always help a beginner like me. :)
0

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'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.